utilities/test/index.js

282 lines
5.4 KiB
JavaScript
Raw Normal View History

2020-03-04 13:31:32 +01:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of utilities which is released under MIT.
2020-03-25 17:01:22 +01:00
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
2020-03-04 13:31:32 +01:00
*/
/* 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);
});
});
2020-03-10 10:46:12 +01:00
test ('copy object', (t) => {
2020-06-29 11:30:16 +02:00
const obj = { foo: 'bar', bar: { foo: 'baz' } };
2020-03-10 10:51:01 +01:00
const copy = util.copy_object (obj);
2020-06-29 11:30:16 +02:00
t.deepEqual (obj, copy);
2020-03-10 10:46:12 +01:00
copy.foo = 'baz';
2020-06-29 11:30:16 +02:00
copy.bar.foo = 'bar';
2020-03-10 10:51:01 +01:00
t.is (copy.foo, 'baz');
2020-06-29 11:30:16 +02:00
t.is (copy.bar.foo, 'bar');
2020-03-10 10:51:01 +01:00
t.is (obj.foo, 'bar');
2020-06-29 11:30:16 +02:00
t.is (obj.bar.foo, 'baz');
2020-03-10 10:46:12 +01:00
});
2020-03-30 11:10:29 +02:00
test ('run regex', (t) => {
const data = 'foobarfoo';
const regex = /foo/gu;
let count = 0;
2020-03-30 11:21:05 +02:00
util.run_regex (regex, data, (res) => {
t.is (res[0], 'foo');
2020-03-30 11:10:29 +02:00
count++;
2020-03-30 11:21:05 +02:00
});
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;
2020-03-30 14:09:23 +02:00
util.run_regex (regex, data, () => {
2020-03-30 11:21:05 +02:00
count++;
});
t.is (count, 0);
2020-03-30 11:10:29 +02:00
});
2020-04-28 08:27:06 +02:00
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);
});
2020-06-08 14:44:09 +02:00
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);
});
2020-06-26 16:11:30 +02:00
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' }
]
}
];
2020-06-26 16:12:16 +02:00
const filtered = [ { name: 'foo', f: 'include' } ];
2020-06-26 16:11:30 +02:00
const filter = {
2020-06-29 14:25:03 +02:00
field: [
2020-06-26 16:12:16 +02:00
'name',
'f'
],
2020-06-26 16:11:30 +02:00
filter: /foo include/ui
};
const result = util.recursive_filter (raw, [ filter ]);
t.deepEqual (filtered, result);
});
2020-06-29 12:37:28 +02:00
test ('recursive filter undefined multifield', (t) => {
const res = util.recursive_filter (
[ { foo: 'bar' } ],
[
{
2020-06-29 14:25:03 +02:00
field: [
2020-06-29 12:37:28 +02:00
'foo',
'bar'
],
filter: /\s/u
}
]
);
t.deepEqual (res, []);
});
2020-06-30 10:00:36 +02:00
test ('recursive filter with or group', (t) => {
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);
t.deepEqual (res, to_filter.slice (0, 2));
});
2020-06-30 11:20:09 +02:00
test ('recursive filter carry field', (t) => {
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' ]
);
t.deepEqual (res, [
{
name: 'foo',
children: [ { name: 'bar' } ]
}
]);
});
2020-07-03 14:36:39 +02:00
test ('recursive filter custom function', (t) => {
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' ]
);
t.deepEqual (res, [
{
name: 'foo',
children: [ { name: 'bar' } ]
}
]);
});