191 lines
3.8 KiB
JavaScript
191 lines
3.8 KiB
JavaScript
/*
|
|
* 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
|
|
*/
|
|
|
|
/* eslint-disable no-magic-numbers */
|
|
// @ts-nocheck
|
|
'use strict';
|
|
|
|
const test = require ('ava');
|
|
const util = require ('../index');
|
|
|
|
test ('truncate_decimal', (t) => {
|
|
const trunc = util.truncate_decimal (1.23456, 2);
|
|
t.is (trunc, 1.23);
|
|
});
|
|
|
|
test ('try_parse_json should parse', (t) => {
|
|
const str = '{"test":"foo"}';
|
|
t.notThrows (() => {
|
|
const json = util.try_parse_json (str);
|
|
t.deepEqual (json, { test: 'foo' });
|
|
});
|
|
});
|
|
|
|
test ('try_parse_json should fail', (t) => {
|
|
const str = '{"test":foo"}';
|
|
t.notThrows (() => {
|
|
const json = util.try_parse_json (str);
|
|
t.is (json, null);
|
|
});
|
|
});
|
|
|
|
test ('copy object', (t) => {
|
|
const obj = { foo: 'bar', bar: { foo: 'baz' } };
|
|
const copy = util.copy_object (obj);
|
|
t.deepEqual (obj, copy);
|
|
copy.foo = 'baz';
|
|
copy.bar.foo = 'bar';
|
|
t.is (copy.foo, 'baz');
|
|
t.is (copy.bar.foo, 'bar');
|
|
t.is (obj.foo, 'bar');
|
|
t.is (obj.bar.foo, 'baz');
|
|
});
|
|
|
|
test ('run regex', (t) => {
|
|
const data = 'foobarfoo';
|
|
const regex = /foo/gu;
|
|
let count = 0;
|
|
util.run_regex (regex, data, (res) => {
|
|
t.is (res[0], 'foo');
|
|
count++;
|
|
});
|
|
t.is (count, 2);
|
|
});
|
|
|
|
test ('run non-global regex', (t) => {
|
|
const data = 'foobarfoo';
|
|
const regex = /foo/u;
|
|
let count = 0;
|
|
util.run_regex (regex, data, (res) => {
|
|
t.is (res[0], 'foo');
|
|
count++;
|
|
});
|
|
t.is (count, 1);
|
|
});
|
|
|
|
test ('run non-global regex without result', (t) => {
|
|
const data = 'foobarfoo';
|
|
const regex = /baz/u;
|
|
let count = 0;
|
|
util.run_regex (regex, data, () => {
|
|
count++;
|
|
});
|
|
t.is (count, 0);
|
|
});
|
|
|
|
test ('check isnil with undefined', (t) => {
|
|
t.is (util.is_nil (), true);
|
|
});
|
|
|
|
test ('check isnil with null', (t) => {
|
|
t.is (util.is_nil (null), true);
|
|
});
|
|
|
|
test ('check isnil with empty string', (t) => {
|
|
t.is (util.is_nil (''), false);
|
|
});
|
|
|
|
test ('check isnil with string', (t) => {
|
|
t.is (util.is_nil ('foo'), false);
|
|
});
|
|
|
|
test ('check isnil with 0', (t) => {
|
|
t.is (util.is_nil (0), false);
|
|
});
|
|
|
|
test ('check isnil with nan', (t) => {
|
|
t.is (util.is_nil (parseInt ('foo')), true);
|
|
});
|
|
|
|
test ('check isnil with int', (t) => {
|
|
t.is (util.is_nil (parseInt ('42')), false);
|
|
});
|
|
|
|
test ('recursive filter', (t) => {
|
|
const raw = [
|
|
{ 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 filtered = [
|
|
{ name: 'include_foo' },
|
|
{
|
|
name: 'include_bar',
|
|
children: [
|
|
{ name: 'foo' },
|
|
{ name: 'bar' }
|
|
]
|
|
},
|
|
{
|
|
name: 'baz',
|
|
children: [ { name: 'include_foo' } ]
|
|
}
|
|
];
|
|
const filter = {
|
|
field: 'name',
|
|
filter: /^include_.*/ui
|
|
};
|
|
const result = util.recursive_filter (raw, [ filter ]);
|
|
t.deepEqual (filtered, result);
|
|
});
|
|
|
|
test ('recursive filter multifield', (t) => {
|
|
const raw = [
|
|
{ name: 'foo', f: 'include' },
|
|
{
|
|
name: 'include_bar',
|
|
children: [
|
|
{ name: 'foo' },
|
|
{ name: 'bar' }
|
|
]
|
|
},
|
|
{
|
|
name: 'baz',
|
|
children: [
|
|
{ name: 'include_foo' },
|
|
{ name: 'bar' }
|
|
]
|
|
},
|
|
{
|
|
name: 'barbaz',
|
|
children: [
|
|
{ name: 'foo' },
|
|
{ name: 'bar' }
|
|
]
|
|
}
|
|
];
|
|
const filtered = [ { name: 'foo', f: 'include' } ];
|
|
const filter = {
|
|
fields: [
|
|
'name',
|
|
'f'
|
|
],
|
|
filter: /foo include/ui
|
|
};
|
|
const result = util.recursive_filter (raw, [ filter ]);
|
|
t.deepEqual (filtered, result);
|
|
});
|