Auto Parse

node v4.8.5
version: 1.2.0
endpointsharetweet
var autoParse = require('auto-parse') var assert = require('chai').assert function Color (inputColor) { this.color = inputColor } assert.equal(autoParse('Green Pioneer'), 'Green Pioneer') assert.typeOf(autoParse('Green Pioneer'), 'string') assert.equal(autoParse('Green Pioneer Solutions'), 'Green Pioneer Solutions') assert.typeOf(autoParse('Green Pioneer Solutions'), 'string') assert.equal(autoParse('True or False'), 'True or False') assert.typeOf(autoParse('True or False'), 'string') assert.equal(autoParse('123 Numbers'), '123 Numbers') assert.typeOf(autoParse('123 Numbers'), 'string') assert.equal(autoParse('$group'), '$group') assert.typeOf(autoParse('$group'), 'string') assert.equal(autoParse('a11y'), 'a11y') assert.typeOf(autoParse('a11y'), 'string') assert.equal(autoParse('$group:test'), '$group:test') assert.typeOf(autoParse('$group:test'), 'string') assert.equal(autoParse('got a question?'), 'got a question?') assert.typeOf(autoParse('got a question?'), 'string') assert.equal(autoParse('0xFF'), 255) assert.equal(autoParse('.42'), 0.42) assert.equal(autoParse('0o123'), 83) assert.equal(autoParse('0b1101'), 13) assert.equal(autoParse('7e3'), 7000) assert.equal(autoParse('26'), 26) assert.typeOf(autoParse('26'), 'number') assert.equal(autoParse('1'), 1) assert.typeOf(autoParse('1'), 'number') assert.equal(autoParse('0'), 0) assert.typeOf(autoParse('0'), 'number') assert.equal(autoParse(26), 26) assert.typeOf(autoParse(26), 'number') assert.equal(autoParse(1), 1) assert.typeOf(autoParse(1), 'number') assert.equal(autoParse(0), 0) assert.typeOf(autoParse(0), 'number') assert.equal(autoParse(true), true) assert.typeOf(autoParse(true), 'boolean') assert.equal(autoParse(false), false) assert.typeOf(autoParse(false), 'boolean') assert.equal(autoParse('true'), true) assert.typeOf(autoParse('true'), 'boolean') assert.equal(autoParse('false'), false) assert.typeOf(autoParse('false'), 'boolean') assert.equal(autoParse('True'), true) assert.typeOf(autoParse('True'), 'boolean') assert.equal(autoParse('TrUe'), true) assert.typeOf(autoParse('TrUe'), 'boolean') var data = ['80', 92, '23', 'TruE', false] assert.equal(autoParse(data)[0], 80) assert.typeOf(autoParse(data)[0], 'number') assert.equal(autoParse(data)[1], 92) assert.typeOf(autoParse(data)[1], 'number') assert.equal(autoParse(data)[2], 23) assert.typeOf(autoParse(data)[2], 'number') assert.equal(autoParse(data)[3], true) assert.typeOf(autoParse(data)[3], 'boolean') assert.equal(autoParse(data)[4], false) assert.typeOf(autoParse(data)[4], 'boolean') var data = { name: 'jason', age: '50', admin: 'true', parents: [ { name: 'Alice', age: '75', dead: 'false' }, { name: 'Bob', age: '80', dead: 'true' } ], grade: ['80', '90', '100'] } assert.equal(autoParse(data).name, 'jason') assert.typeOf(autoParse(data).name, 'string') assert.equal(autoParse(data).age, 50) assert.typeOf(autoParse(data).age, 'number') assert.equal(autoParse(data).admin, true) assert.typeOf(autoParse(data).admin, 'boolean') assert.equal(autoParse(data).grade[0], 80) assert.typeOf(autoParse(data).grade[0], 'number') assert.equal(autoParse(data).grade[1], 90) assert.typeOf(autoParse(data).grade[1], 'number') assert.equal(autoParse(data).grade[2], 100) assert.typeOf(autoParse(data).grade[2], 'number') assert.typeOf(autoParse(data).grade[2], 'number') assert.equal(autoParse(data).parents[0].name, 'Alice') assert.equal(autoParse(data).parents[0].age, 75) var data = function () { return '9' } assert.equal(autoParse(data), 9) assert.typeOf(autoParse(data), 'number') var data = function () { return 'jason' } assert.equal(autoParse(data), 'jason') assert.typeOf(autoParse(data), 'string') var data = function () { return 'true' } assert.equal(autoParse(data), true) assert.typeOf(autoParse(data), 'boolean') assert.equal(autoParse(undefined), undefined) assert.typeOf(autoParse(undefined), 'undefined') assert.equal(autoParse('Undefined'), undefined) assert.typeOf(autoParse('Undefined'), 'undefined') assert.equal(autoParse(''), undefined) assert.typeOf(autoParse(''), 'undefined') assert.equal(autoParse('null'), null) assert.typeOf(autoParse('null'), 'null') assert.equal(autoParse('Null'), null) assert.typeOf(autoParse('Null'), 'null') assert.deepEqual(autoParse('[]'), []) assert.deepEqual(autoParse('["42"]'), [42]) assert.deepEqual(autoParse(NaN), NaN) assert.deepEqual(autoParse('NaN'), NaN) assert.deepEqual(autoParse('#AAA', Color), { color: '#AAA' }) assert.deepEqual(autoParse('1989-12-01', Date), new Date('1989-12-01')) assert.deepEqual(autoParse('1989-12-01', 'date'), new Date('1989-12-01')) var data = function () { return '9' } assert.equal(autoParse(1234, String), '1234') assert.equal(autoParse('1234', 'String'), '1234') assert.equal(autoParse(1234, 'String'), '1234') assert.equal(autoParse('true', 'String'), 'true') assert.equal(autoParse(true, 'String'), 'true') assert.equal(autoParse([], 'String'), '[]') assert.equal(autoParse({}, 'String'), '{}') assert.equal(autoParse(data, 'String'), "function () {\n return '9'\n }") var data = function () { return '9' } assert.equal(autoParse(data, 'function'), data) var data = { name: 'jason', age: '50', admin: 'true', parents: [ { name: 'Alice', age: '75', dead: 'false' }, { name: 'Bob', age: '80', dead: 'true' } ], grade: ['80', '90', '100'] } assert.equal(autoParse(data, 'object').name, 'jason') assert.equal(autoParse(data, Object).name, 'jason') assert.typeOf(autoParse(data, 'object').name, 'string') assert.equal(autoParse(data, 'object').age, 50) assert.typeOf(autoParse(data, 'object').age, 'number') assert.equal(autoParse(data, 'object').admin, true) assert.typeOf(autoParse(data, 'object').admin, 'boolean') assert.equal(autoParse(data, 'object').grade[0], 80) assert.typeOf(autoParse(data, 'object').grade[0], 'number') assert.equal(autoParse(data, 'object').grade[1], 90) assert.typeOf(autoParse(data, 'object').grade[1], 'number') assert.equal(autoParse(data, 'object').grade[2], 100) assert.typeOf(autoParse(data, 'object').grade[2], 'number') assert.typeOf(autoParse(data, 'object').grade[2], 'number') assert.equal(autoParse(data, 'object').parents[0].name, 'Alice') assert.equal(autoParse(data, 'object').parents[0].age, 75) assert.deepEqual(autoParse('[]', 'object'), []) assert.deepEqual(autoParse('["42"]', 'object'), [42]) assert.equal(autoParse(1, Boolean), true) assert.equal(autoParse(1, 'Boolean'), true) assert.equal(autoParse(0, 'Boolean'), false) assert.equal(autoParse('1', 'Boolean'), true) assert.equal(autoParse('0', 'Boolean'), false) assert.equal(autoParse('TrUe', 'Boolean'), true) assert.equal(autoParse('False', 'Boolean'), false) assert.equal(autoParse(true, 'Boolean'), true) assert.equal(autoParse(false, 'Boolean'), false) assert.equal(autoParse('foo', 'Boolean'), false) assert.equal(autoParse(1, 'Number'), 1) assert.equal(autoParse(0, 'Number'), 0) assert.equal(autoParse(0, Number), 0) assert.equal(autoParse('1', Number), 1) assert.equal(autoParse('1', 'Number'), 1) assert.equal(autoParse('0', 'Number'), 0) assert.equal(autoParse('0o123', 'Number'), 83) assert.equal(autoParse('0b10', 'Number'), 2) assert.equal(autoParse('0xFF', 'Number'), 255) assert.equal(autoParse('7e3', 'Number'), 7000) assert.equal(autoParse('.42', 'Number'), 0.42) assert.equal(autoParse(1, 'Undefined'), undefined) assert.equal(autoParse(0, 'Undefined'), undefined) assert.equal(autoParse('1', 'Undefined'), undefined) assert.equal(autoParse('0', 'Undefined'), undefined) assert.equal(autoParse(1, 'Null'), null) assert.equal(autoParse(0, 'Null'), null) assert.equal(autoParse('1', 'Null'), null) assert.equal(autoParse('0', 'Null'), null)
Loading…

no comments

    sign in to comment