-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize and deserialize functions and special values
- Loading branch information
Showing
5 changed files
with
179 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
exports.parseClientValue = parseClientValue; | ||
|
||
function parseClientValue(value) { | ||
if (value === null) { | ||
return null; | ||
} | ||
switch (typeof value) { | ||
case 'string': | ||
if (value === '[undefined]') { | ||
return undefined; | ||
} | ||
if (value === '[NaN]') { | ||
return NaN; | ||
} | ||
if (value === '[Infinity]') { | ||
return Infinity; | ||
} | ||
if (value === '[-Infinity]') { | ||
return -Infinity; | ||
} | ||
if (value.startsWith('[Function: ')) { | ||
return makeFunction(value.slice(11, -1)); | ||
} | ||
if (value.startsWith('Symbol(')) { | ||
return Symbol(value.slice(7, -1)); | ||
} | ||
return value; | ||
case 'object': { | ||
const new_value = Array.isArray(value) ? [] : {}; | ||
for (const [key, value2] of Object.entries(value)) { | ||
new_value[key] = parseClientValue(value2); | ||
} | ||
return new_value; | ||
} | ||
default: | ||
return value; | ||
} | ||
} | ||
|
||
function makeFunction(name) { | ||
const fn = function () {}; | ||
Object.defineProperty(fn, 'name', { value: name }); | ||
return fn; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'use strict'; | ||
|
||
const { assert } = require('@sinonjs/referee-sinon'); | ||
const { parseClientValue } = require('./parse-client-value'); | ||
|
||
describe('lib/parse-client-value', () => { | ||
it('returns null for null', () => { | ||
assert.isNull(parseClientValue(null)); | ||
}); | ||
|
||
it('returns true for true', () => { | ||
assert.isTrue(parseClientValue(true)); | ||
}); | ||
|
||
it('returns false for false', () => { | ||
assert.isFalse(parseClientValue(false)); | ||
}); | ||
|
||
it('returns number for number', () => { | ||
assert.equals(parseClientValue(0), 0); | ||
assert.equals(parseClientValue(-7), -7); | ||
assert.equals(parseClientValue(42), 42); | ||
}); | ||
|
||
it('returns string as is', () => { | ||
assert.equals(parseClientValue(''), ''); | ||
assert.equals(parseClientValue('test'), 'test'); | ||
assert.equals(parseClientValue('[test]'), '[test]'); | ||
}); | ||
|
||
it('returns undefined for [undefined]', () => { | ||
assert.isUndefined(parseClientValue('[undefined]')); | ||
}); | ||
|
||
it('returns Infinity for [Infinity]', () => { | ||
assert.isInfinity(parseClientValue('[Infinity]')); | ||
}); | ||
|
||
it('returns -Infinity for [-Infinity]', () => { | ||
assert.isNegativeInfinity(parseClientValue('[-Infinity]')); | ||
}); | ||
|
||
it('returns NaN for [NaN]', () => { | ||
assert.isNaN(parseClientValue('[NaN]')); | ||
}); | ||
|
||
it('returns symbol for Symbol()', () => { | ||
const value = parseClientValue('Symbol()'); | ||
|
||
assert.isSymbol(value); | ||
assert.equals(value.toString(), 'Symbol()'); | ||
}); | ||
|
||
it('returns symbol for Symbol(test)', () => { | ||
const value = parseClientValue('Symbol(test)'); | ||
|
||
assert.isSymbol(value); | ||
assert.equals(value.toString(), 'Symbol(test)'); | ||
}); | ||
|
||
it('returns anonymous function for [Function: ]', () => { | ||
const value = parseClientValue('[Function: ]'); | ||
|
||
assert.isFunction(value); | ||
assert.equals(value.name, ''); | ||
}); | ||
|
||
it('returns named function for [Function: test]', () => { | ||
const value = parseClientValue('[Function: test]'); | ||
|
||
assert.isFunction(value); | ||
assert.equals(value.name, 'test'); | ||
}); | ||
|
||
it('returns object for object', () => { | ||
assert.equals(parseClientValue({}), {}); | ||
assert.equals(parseClientValue({ test: 42 }), { test: 42 }); | ||
assert.equals(parseClientValue({ test: '[NaN]' }), { test: NaN }); | ||
}); | ||
|
||
it('returns array for array', () => { | ||
assert.equals(parseClientValue([]), []); | ||
assert.equals(parseClientValue([7, 42]), [7, 42]); | ||
assert.equals(parseClientValue(['[NaN]']), [NaN]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters