utilities/test/spec/index.js

310 lines
6.4 KiB
JavaScript
Raw Permalink Normal View History

2020-10-05 20:37:48 +02:00
/*
* 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
*/
// @ts-nocheck
'use strict';
const util = require ('../../index');
// eslint-disable-next-line max-lines-per-function
describe ('utilities', () => {
it ('should truncate_decimal', () => {
const trunc = util.truncate_decimal (1.23456, 2);
expect (trunc)
.toEqual (1.23);
});
it ('try_parse_json should parse', () => {
const str = '{"test":"foo"}';
expect (() => {
const json = util.try_parse_json (str);
expect (json)
.toEqual ({ test: 'foo' });
}).not.toThrow ();
});
it ('try_parse_json should fail', () => {
const str = '{"test":foo"}';
expect (() => {
const json = util.try_parse_json (str);
expect (json)
.toBeNull ();
}).not.toThrow ();
});
it ('should copy object', () => {
const obj = { foo: 'bar', bar: { foo: 'baz' } };
const copy = util.copy_object (obj);
expect (obj).not.toBe (copy);
expect (obj)
.toEqual (copy);
copy.foo = 'baz';
copy.bar.foo = 'bar';
expect (copy.foo)
.toEqual ('baz');
expect (copy.bar.foo)
.toEqual ('bar');
expect (obj.foo)
.toEqual ('bar');
expect (obj.bar.foo)
.toEqual ('baz');
});
it ('should run regex', () => {
const data = 'foobarfoo';
const regex = /foo/gu;
let count = 0;
util.run_regex (regex, data, (res) => {
expect (res[0])
.toEqual ('foo');
count++;
});
expect (count)
.toEqual (2);
});
it ('should run non-global regex', () => {
const data = 'foobarfoo';
const regex = /foo/u;
let count = 0;
util.run_regex (regex, data, (res) => {
expect (res[0])
.toEqual ('foo');
count++;
});
expect (count)
.toEqual (1);
});
it ('should run non-global regex without result', () => {
const data = 'foobarfoo';
const regex = /baz/u;
let count = 0;
util.run_regex (regex, data, () => {
count++;
});
expect (count)
.toEqual (0);
});
it ('should check isnil with undefined', () => {
expect (util.is_nil ())
.toEqual (true);
});
it ('should check isnil with null', () => {
expect (util.is_nil (null))
.toEqual (true);
});
it ('should check isnil with empty string', () => {
expect (util.is_nil (''))
.toEqual (false);
});
it ('should check isnil with string', () => {
expect (util.is_nil ('foo'))
.toEqual (false);
});
it ('should check isnil with 0', () => {
expect (util.is_nil (0))
.toEqual (false);
});
it ('should check isnil with nan', () => {
expect (util.is_nil (parseInt ('foo')))
.toEqual (true);
});
it ('should check isnil with int', () => {
expect (util.is_nil (parseInt ('42')))
.toEqual (false);
});
it ('recursive filter', () => {
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 ]);
expect (filtered)
.toEqual (result);
});
it ('recursive filter multifield', () => {
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 = {
field: [
'name',
'f'
],
filter: /foo include/ui
};
const result = util.recursive_filter (raw, [ filter ]);
expect (filtered)
.toEqual (result);
});
it ('recursive filter undefined multifield', () => {
const res = util.recursive_filter (
[ { foo: 'bar' } ],
[
{
field: [
'foo',
'bar'
],
filter: /\s/u
}
]
);
expect (res)
.toEqual ([]);
});
it ('recursive filter with or group', () => {
const to_filter = [
{ name: 'foo' },
{ name: 'bar' },
{ name: 'baz' }
];
const filter = [
{
or: [
{ field: 'name', filter: /foo/u },
{ field: 'name', filter: /bar/u }
]
}
];
const res = util.recursive_filter (to_filter, filter);
expect (res)
.toEqual (to_filter.slice (0, 2));
});
it ('recursive filter carry field', () => {
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 } ],
'children',
[ 'name' ]
);
expect (res)
.toEqual ([
{
name: 'foo',
children: [ { name: 'bar' } ]
}
]);
});
it ('recursive filter custom function', () => {
const to_filter = [
{
name: 'foo',
children: [
{ name: 'bar' },
{ name: 'baz' },
{ foo: 'bar' }
]
}
];
const res = util.recursive_filter (
to_filter,
[ { function: (elem) => elem.name === 'foo bar' } ],
'children',
[ 'name' ]
);
expect (res)
.toEqual ([
{
name: 'foo',
children: [ { name: 'bar' } ]
}
]);
});
});