utilities/test/index.js
2020-03-30 11:10:29 +02:00

54 lines
1.2 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>, March 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' };
const copy = util.copy_object (obj);
copy.foo = 'baz';
t.is (copy.foo, 'baz');
t.is (obj.foo, 'bar');
});
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);
});