2020-03-06 08:35:03 +01:00
|
|
|
# Utilities
|
|
|
|
|
|
|
|
General helper functions
|
|
|
|
|
2020-03-06 08:38:41 +01:00
|
|
|
```js
|
2020-03-06 08:35:03 +01:00
|
|
|
const util = require('@scode/utilities');
|
|
|
|
|
|
|
|
// cut off decimal places to a specified point
|
|
|
|
util.truncate_decimal(12.345678, 2);
|
|
|
|
// returns 12.34
|
|
|
|
|
|
|
|
// will return null instead of throwing on invalid json
|
|
|
|
util.try_parse_json('{{foo');
|
2020-03-10 10:46:12 +01:00
|
|
|
|
|
|
|
// copy an object to prevent modification of the original
|
|
|
|
const obj = {foo:'bar'};
|
|
|
|
const copy = util.copy_object(obj);
|
|
|
|
copy.foo = 'baz';
|
|
|
|
console.log(obj.foo); // bar
|
2020-03-30 11:00:52 +02:00
|
|
|
|
|
|
|
// run a regular expression and get a callback for every result
|
|
|
|
const data = "foobarfoo";
|
|
|
|
const regex = /foo/g;
|
|
|
|
util.run_regex(regex, data, res => {
|
|
|
|
console.log(res[0]); // will output 'foo' 2 times
|
|
|
|
});
|
2020-03-06 08:35:03 +01:00
|
|
|
```
|