utilities/README.md

184 lines
3.4 KiB
Markdown
Raw Normal View History

# @sapphirecode/utilities
2020-03-06 08:35:03 +01:00
2020-06-30 10:00:36 +02:00
version: 1.6.x
small utility functions to make much needed features easier to work with
## Installation
npm:
> npm i --save @sapphirecode/utilities
yarn:
2020-06-08 14:44:09 +02:00
> yarn add @sapphirecode/utilities
## Usage
2020-03-06 08:35:03 +01:00
2020-03-06 08:38:41 +01:00
```js
const util = require('@sapphirecode/utilities');
2020-06-08 14:44:09 +02:00
```
cut off decimal places to a specified point
2020-03-06 08:35:03 +01:00
2020-06-08 14:44:09 +02:00
```js
2020-03-06 08:35:03 +01:00
util.truncate_decimal(12.345678, 2);
// returns 12.34
2020-06-08 14:44:09 +02:00
```
will return null instead of throwing on invalid json
2020-03-06 08:35:03 +01:00
2020-06-08 14:44:09 +02:00
```js
2020-03-06 08:35:03 +01:00
util.try_parse_json('{{foo');
2020-06-08 14:44:09 +02:00
```
copy an object to prevent modification of the original
2020-03-10 10:46:12 +01:00
2020-06-08 14:44:09 +02:00
```js
const obj = {foo: 'bar'};
2020-03-10 10:46:12 +01:00
const copy = util.copy_object(obj);
copy.foo = 'baz';
console.log(obj.foo); // bar
2020-06-08 14:44:09 +02:00
```
2020-03-30 11:00:52 +02:00
2020-06-08 14:44:09 +02:00
run a regular expression and get a callback for every result
```js
const data = 'foobarfoo';
2020-03-30 11:00:52 +02:00
const regex = /foo/g;
2020-06-08 14:44:09 +02:00
util.run_regex(regex, data, (res) => {
2020-03-30 11:00:52 +02:00
console.log(res[0]); // will output 'foo' 2 times
});
2020-06-08 14:44:09 +02:00
```
2020-06-08 14:44:09 +02:00
check if a variable is null, undefined or NaN
```js
console.log(util.is_nil(parseInt('abc'))); // true
console.log(util.is_nil('foo')); // false
console.log(util.is_nil(42)); // false
console.log(util.is_nil(null)); // true
console.log(util.is_nil(undefined)); // true
2020-03-06 08:35:03 +01:00
```
2020-06-08 14:44:09 +02:00
filter an array recursively
```js
const to_filter = [
{name: 'include_foo'},
{
name: 'include_bar',
children: [{name: 'foo'}, {name: 'bar'}],
},
{
name: 'baz',
children: [{name: 'include_foo'}, {name: 'bar'}],
},
{
name: 'barbaz',
children: [{name: 'foo'}, {name: 'bar'}],
},
];
const filter = {
field: 'name', // the field the filter will apply on
filter: /^include_.*/iu, // regex filter
};
const result = util.recursive_filter(
to_filter,
[filter], // you can specify multiple filters, they will be connected using AND
'children' // specify which field an objects children are stored in ('children' is default)
);
console.log(JSON.stringify(result, null, 2));
/* output:
[
{ name: 'include_foo' },
{
name: 'include_bar',
children: [
{ name: 'foo' },
{ name: 'bar' }
]
},
{
name: 'baz',
children: [ { name: 'include_foo' } ]
}
]*/
```
2020-06-26 16:11:30 +02:00
filters can also apply to multiple fields
```js
const result = util.recursive_filter(
[{name: 'foo', name_2: 'bar'}],
[
{
filter: /foo bar/iu,
2020-06-30 11:20:09 +02:00
field: ['name', 'name_2'], // fields will be joined with a space in between
2020-06-26 16:11:30 +02:00
// {name: 'foo', name_2: 'bar'} will become 'foo bar'
2020-06-30 11:20:09 +02:00
},
2020-06-26 16:11:30 +02:00
]
);
```
2020-06-30 10:00:36 +02:00
filter groups can be used to connect filters with OR
```js
const result = util.recursive_filter(
[...],
[
{
filter: /foo bar/iu,
field: 'name'
2020-06-30 11:20:09 +02:00
},
2020-06-30 10:00:36 +02:00
{
or: [
{ filter: /foo/u, field: 'bar' },
{ filter: /bar/u, field: 'bar' }
]
}
]
);
```
2020-06-30 11:20:09 +02:00
field values can be carried to filter runs on children to filter over the entire tree instead of just one element
```js
const to_filter = [
{
name: 'foo',
children: [
{ name: 'bar' },
{ name: 'baz' },
{ foo: 'bar' }
]
}
];
const res = util.recursive_filter (
to_filter,
[ { field: 'name', filter: /foo bar/ui } ], // values will be separaed by a space
'children',
[ 'name' ] // specify which fields should be carried
// the filters will receive the carried values instead of the plain fields
);
/* result:
[
{
name: 'foo',
children: [ { name: 'bar' } ]
}
]
*/
```
2020-06-30 10:00:36 +02:00
## License
MIT © Timo Hocker <timo@scode.ovh>