jsharmony
Version:
Rapid Application Development (RAD) Platform for Node.js Database Application Development
180 lines (173 loc) • 7.07 kB
JavaScript
/*
Copyright 2017 apHarmony
This file is part of jsHarmony.
jsHarmony is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
jsHarmony is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this package. If not, see <http://www.gnu.org/licenses/>.
*/
var XFormat = require('../clientjs/XFormat.js')();
var assert = require('assert');
var _ = require('lodash');
function testFormat(encode, decode, tests){
_.each(tests, function(test){
test.args = test.args||[];
test.auxin = test.auxin||[];
var xargs = (test.args).slice(0);
xargs.unshift(XFormat);
var xencode = encode.bind.apply(encode, xargs);
var xdecode = decode.bind.apply(decode, xargs);
assert.strictEqual(xencode(test.in),test.out,'Encode(In)==Out '+test.in);
assert.strictEqual(xdecode(xencode(test.in)),xdecode(test.in),'Decode(Encode(In))==Decode(In) '+test.in);
assert.strictEqual(xdecode(xdecode(xencode(test.in))),xdecode(test.in),'Decode(Decode(Encode(In)))==Decode(In) '+test.in);
_.each(test.auxin, function(auxin){
assert.strictEqual(xdecode(auxin),xdecode(test.in),'Decode(AuxIn)==Decode(In) '+auxin);
assert.strictEqual(xdecode(xdecode(auxin)),xdecode(test.in),'Decode(Decode(AuxIn))==Decode(In) '+auxin);
assert.strictEqual(xdecode(xdecode(auxin)),xdecode(auxin),'Decode(Decode(AuxIn))==Decode(AuxIn) '+auxin);
});
});
}
describe('XFormat',function(){
it('phone', function () {
testFormat(XFormat.phone, XFormat.phone_decode, [
{ in: '2342342345', out: '(234) 234-2345', auxin: ['1-234-234-2345', '11112342342345','234 234 2345'] },
{ in: '', out: '' },
{ in: null, out: null },
]);
});
//date(format)
it('date', function () {
testFormat(XFormat.date, XFormat.date_decode, [
{ in: '2019-02-21T00:00:00.000', out: '02/21/2019', args: ['MM/DD/YYYY'], auxin: ['2019-02-21','2/21/2019'] },
{ in: '', out: '', args: ['MM/DD/YYYY'] },
{ in: null, out: null, args: ['MM/DD/YYYY'] },
]);
});
//tstmp
it('tstmp', function () {
testFormat(XFormat.tstmp, XFormat.tstmp_decode, [
{ in: '2019-02-21T14:02:00.000', out: '02/21/19 14:02', auxin: [] },
{ in: '', out: '' },
{ in: null, out: null },
]);
});
//MMDDYY
it('MMDDYY', function () {
testFormat(XFormat.MMDDYY, XFormat.MMDDYY_decode, [
{ in: '2019-02-21T00:00:00.000', out: '02/21/19', auxin: [] },
{ in: '', out: '' },
{ in: null, out: null },
]);
});
//decimal(numdigits)
it('decimal', function () {
testFormat(XFormat.decimal, XFormat.decimal, [
{ in: 1, out: '1.00', args: [2] },
{ in: 1111, out: '1111.00', args: [2] },
{ in: 1.11, out: '1.11', args: [2] },
{ in: 1.1111, out: '1.11', args: [2] },
{ in: 1.116, out: '1.12', args: [2] },
{ in: 1111.1111, out: '1111.11', args: [2], auxin: ['1111.1111 \t'] },
{ in: 11111111, out: '11111111.00', args: [2] },
{ in: 11111111.1111, out: '11111111.11', args: [2] },
{ in: '.1111', out: '0.11', args: [2] },
{ in: '11111111111', out: '11111111111.00', args: [2] },
{ in: '', out: '', args: [2] },
{ in: null, out: null, args: [2] },
]);
});
//decimalext(numdigits)
it('decimalext', function () {
testFormat(XFormat.decimalext, XFormat.decimalext, [
{ in: 1, out: '1.00', args: [2] },
{ in: 1111, out: '1111.00', args: [2] },
{ in: 1.11, out: '1.11', args: [2] },
{ in: 1.1111, out: '1.1111', args: [2] },
{ in: 1.116, out: '1.116', args: [2] },
{ in: 1111.1111, out: '1111.1111', args: [2], auxin: ['1111.1111 \t'] },
{ in: 11111111, out: '11111111.00', args: [2] },
{ in: 11111111.1111, out: '11111111.1111', args: [2] },
{ in: '.1111', out: '0.1111', args: [2] },
{ in: '11111111111', out: '11111111111.00', args: [2] },
{ in: '', out: '', args: [2] },
{ in: null, out: null, args: [2] },
]);
});
//decimalcomma(numdigits)
it('decimalcomma', function () {
testFormat(XFormat.decimalcomma, XFormat.decimalcomma_decode, [
{ in: 1, out: '1.00', args: [2] },
{ in: 1111, out: '1,111.00', args: [2] },
{ in: 1.11, out: '1.11', args: [2] },
{ in: 1.1111, out: '1.11', args: [2] },
{ in: 1.116, out: '1.12', args: [2] },
{ in: 1111.1111, out: '1,111.11', args: [2], auxin: ['1111.1111 \t'] },
{ in: 11111111, out: '11,111,111.00', args: [2] },
{ in: 11111111.1111, out: '11,111,111.11', args: [2] },
{ in: '.1111', out: '0.11', args: [2] },
{ in: '11111111111', out: '11,111,111,111.00', args: [2] },
{ in: '', out: '', args: [2] },
]);
});
//comma
it('comma', function () {
testFormat(XFormat.comma, XFormat.comma_decode, [
{ in: '1', out: '1' },
{ in: '1111', out: '1,111' },
{ in: '1.1111', out: '1.1111' },
{ in: '1111.1111', out: '1,111.1111', auxin: ['1111.1111 \t'] },
{ in: '11111111', out: '11,111,111' },
{ in: '11111111.1111', out: '11,111,111.1111' },
{ in: '.1111', out: '.1111' },
{ in: '11111111111', out: '11,111,111,111' },
{ in: '', out: '' },
]);
});
//ssn
it('ssn', function () {
testFormat(XFormat.ssn, XFormat.ssn_decode, [
{ in: '222334444', out: '222-33-4444', auxin: ['222 33 4444'] },
{ in: '', out: '' },
{ in: null, out: null },
]);
});
//ein
it('ein', function () {
testFormat(XFormat.ein, XFormat.ein_decode, [
{ in: '223333333', out: '22-3333333', auxin: ['22 3333333'] },
{ in: '', out: '' },
{ in: null, out: null },
]);
});
//time
it('time', function () {
testFormat(XFormat.time, XFormat.time_decode, [
{ in: '1970-01-01T15:02:01.123', out: '03:02:01.123 pm', args: ['hh:mm:ss.SSS a'], auxin: ['15:02:01.123'] },
{ in: '', out: '', args: ['hh:mm:ss.SSS a'], auxin: [null] },
]);
});
//bool
it('bool', function () {
testFormat(XFormat.bool, XFormat.bool_decode, [
{ in: true, out: 'true', auxin: ['TRUE','T','Y','YES','ON','1'] },
{ in: false, out: 'false', auxin: ['FALSE','F','N','NO','OFF','0', '', null] },
]);
});
//json
it('json', function () {
testFormat(XFormat.json, XFormat.json_decode, [
{ in: '', out: '' },
{ in: null, out: null },
{ in: '{}', out: '{}'},
{ in: '{"k":"v"}', out: '{\n "k": "v"\n}'},
{ in: 1, out: '1'},
{ in: {}, out: '{}'},
]);
});
});