Compare commits
3 Commits
af709965c5
...
8eb807714c
Author | SHA1 | Date | |
---|---|---|---|
8eb807714c | |||
d8ee6074fa | |||
c5aef0a81e |
44
index.d.ts
vendored
44
index.d.ts
vendored
@ -1,44 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
||||||
* This file is part of utilities which is released under MIT.
|
|
||||||
* See file 'LICENSE' for full license details.
|
|
||||||
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* truncates a floating point number
|
|
||||||
*
|
|
||||||
* @param {number} num number to truncate
|
|
||||||
* @param {number} len length to truncate to
|
|
||||||
* @returns {number} truncated number
|
|
||||||
*/
|
|
||||||
export function truncate_decimal(num: number, len: number): number;
|
|
||||||
/**
|
|
||||||
* parse json and catch invalid strings
|
|
||||||
*
|
|
||||||
* @param {string} text input
|
|
||||||
* @returns {any} parsed
|
|
||||||
*/
|
|
||||||
export function try_parse_json(text: string): any;
|
|
||||||
/**
|
|
||||||
* copy an object to prevent modification to the original
|
|
||||||
*
|
|
||||||
* @param {object} obj object to copy
|
|
||||||
* @returns {object} copy
|
|
||||||
*/
|
|
||||||
export function copy_object(obj: any): any;
|
|
||||||
/**
|
|
||||||
* run a regular expression and callback for every result
|
|
||||||
*
|
|
||||||
* @param {any} regex regular expression
|
|
||||||
* @param {any} data data to run on
|
|
||||||
* @param {any} func function to execute
|
|
||||||
*/
|
|
||||||
export function run_regex(regex: any, data: any, func: any): void;
|
|
||||||
/**
|
|
||||||
* check if an object is either undefined, null or NaN
|
|
||||||
*
|
|
||||||
* @param {any} obj object to check
|
|
||||||
* @returns {boolean} true if nil
|
|
||||||
*/
|
|
||||||
export function is_nil(obj: any): boolean;
|
|
6
index.js
6
index.js
@ -86,9 +86,11 @@ function is_nil (obj) {
|
|||||||
* @returns {Array<object>} filtered data
|
* @returns {Array<object>} filtered data
|
||||||
*/
|
*/
|
||||||
function recursive_filter (input, filters, children_key = 'children') {
|
function recursive_filter (input, filters, children_key = 'children') {
|
||||||
const data = copy_object (input);
|
const data = [ ...input ];
|
||||||
const filtered = [];
|
const filtered = [];
|
||||||
for (const e of data) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
|
const e = { ...data[i] };
|
||||||
|
data[i] = e;
|
||||||
let match = true;
|
let match = true;
|
||||||
for (const filter of filters) {
|
for (const filter of filters) {
|
||||||
let search_str = '';
|
let search_str = '';
|
||||||
|
@ -33,5 +33,8 @@
|
|||||||
"LICENSE",
|
"LICENSE",
|
||||||
"index.js",
|
"index.js",
|
||||||
"index.d.ts"
|
"index.d.ts"
|
||||||
]
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10"
|
||||||
|
}
|
||||||
}
|
}
|
@ -34,11 +34,15 @@ test ('try_parse_json should fail', (t) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test ('copy object', (t) => {
|
test ('copy object', (t) => {
|
||||||
const obj = { foo: 'bar' };
|
const obj = { foo: 'bar', bar: { foo: 'baz' } };
|
||||||
const copy = util.copy_object (obj);
|
const copy = util.copy_object (obj);
|
||||||
|
t.deepEqual (obj, copy);
|
||||||
copy.foo = 'baz';
|
copy.foo = 'baz';
|
||||||
|
copy.bar.foo = 'bar';
|
||||||
t.is (copy.foo, 'baz');
|
t.is (copy.foo, 'baz');
|
||||||
|
t.is (copy.bar.foo, 'bar');
|
||||||
t.is (obj.foo, 'bar');
|
t.is (obj.foo, 'bar');
|
||||||
|
t.is (obj.bar.foo, 'baz');
|
||||||
});
|
});
|
||||||
|
|
||||||
test ('run regex', (t) => {
|
test ('run regex', (t) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user