Skip to content

Commit

Permalink
lib, test: remove JSOS
Browse files Browse the repository at this point in the history
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
nechaido committed May 22, 2017
1 parent 74e7660 commit 9853396
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 337 deletions.
1 change: 0 additions & 1 deletion jstp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = jstp;

Object.assign(jstp,
require('./lib/record-serialization'),
require('./lib/object-serialization'),
require('./lib/errors'),
require('./lib/applications')
);
Expand Down
84 changes: 84 additions & 0 deletions lib/json5-serialize.js
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 + '}';
}
};
44 changes: 0 additions & 44 deletions lib/object-serialization.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/record-serialization-fallback.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

const serializerFactory = require('./serializer-factory');
const serialize = require('./json5-serialize');

const jsrs = {};
module.exports = jsrs;

// Serialize a JavaScript value using the JSTP Record Serialization format
// and return a string representing it.
//
jsrs.stringify = serializerFactory.createSerializer();
jsrs.stringify = serialize;

// Deserialize a string in the JSTP Record Serialization format into
// a JavaScript value and return it.
Expand Down
4 changes: 2 additions & 2 deletions lib/record-serialization.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const safeRequire = require('./common').safeRequire;
const serializerFactory = require('./serializer-factory');
const serialize = require('./json5-serialize');

const jsrs = {};
module.exports = jsrs;
Expand All @@ -19,7 +19,7 @@ const jstpNative =
if (jstpNative) {
Object.assign(jsrs, jstpNative);
if (!USE_NATIVE_SERIALIZER) {
jsrs.stringify = serializerFactory.createSerializer();
jsrs.stringify = serialize;
}
} else {
console.warn(
Expand Down
99 changes: 0 additions & 99 deletions lib/serializer-factory.js

This file was deleted.

Loading

0 comments on commit 9853396

Please sign in to comment.