-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also replace `serializer-factory` by `json5-serializer`. PR-URL: metarhia/jstp#170 Fixes: metarhia/jstp#160 Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com> Reviewed-By: Mykola Bilochub <nbelochub@gmail.com>
- Loading branch information
Showing
7 changed files
with
88 additions
and
337 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use strict'; | ||
|
||
module.exports = serialize; | ||
|
||
function serialize(object) { | ||
let type; | ||
if (Array.isArray(object)) { | ||
type = 'array'; | ||
} else if (object instanceof Date) { | ||
type = 'date'; | ||
} else if (object === null) { | ||
type = 'null'; | ||
} else { | ||
type = typeof(object); | ||
} | ||
|
||
const serializer = serialize.types[type]; | ||
if (serializer) { | ||
return serializer(object); | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
serialize.types = { | ||
number: number => number + '', | ||
boolean: boolean => (boolean ? 'true' : 'false'), | ||
date: date => `'${date.toISOString()}'`, | ||
undefined: () => 'undefined', | ||
null: () => 'null', | ||
|
||
string(string) { | ||
const content = JSON.stringify(string).slice(1, -1); | ||
return `'${content.replace(/'/g, '\\\'')}'`; | ||
}, | ||
|
||
array(array) { | ||
let result = '['; | ||
|
||
for (let index = 0; index < array.length; index++) { | ||
const value = array[index]; | ||
if (value !== undefined) { | ||
result += serialize(value); | ||
} | ||
|
||
if (index !== array.length - 1) { | ||
result += ','; | ||
} | ||
} | ||
|
||
return result + ']'; | ||
}, | ||
|
||
object(object) { | ||
let result = '{'; | ||
let firstKey = true; | ||
|
||
const objectKeys = Object.keys(object); | ||
const objectKeysCount = objectKeys.length; | ||
|
||
for (let i = 0; i < objectKeysCount; i++) { | ||
let key = objectKeys[i]; | ||
const value = serialize(object[key]); | ||
|
||
if (value === '' || value === 'undefined') { | ||
continue; | ||
} | ||
|
||
if (!/^[a-zA-Z_]\w*$/.test(key)) { | ||
key = serialize.types.string(key); | ||
} | ||
|
||
if (firstKey) { | ||
firstKey = false; | ||
} else { | ||
result += ','; | ||
} | ||
|
||
result += key + ':' + value; | ||
} | ||
|
||
return result + '}'; | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.