Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

surrial

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

surrial - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

src/class-constructor.d.ts

17

CHANGELOG.md

@@ -0,1 +1,18 @@

# [2.0.0](https://github.com/nicojs/node-surrial/compare/v1.0.0...v2.0.0) (2020-02-14)
### Features
* **template:** add surrial tag for template literals ([bf27e77](https://github.com/nicojs/node-surrial/commit/bf27e7772af833b799e814387733fcad1c682dc9))
* feat(custom): allow custom serialization logic (#7) ([bd78dc4](https://github.com/nicojs/node-surrial/commit/bd78dc4557191bf105534992dd9445c6973edf0b)), closes [#7](https://github.com/nicojs/node-surrial/issues/7) [#5](https://github.com/nicojs/node-surrial/issues/5)
### BREAKING CHANGES
* If you have `surrialize()` methods on your objects (for some reason?) that function will now be used when serializing the object.
# [1.0.0](https://github.com/nicojs/node-surrial/compare/v0.2.0...v1.0.0) (2019-02-12)

@@ -2,0 +19,0 @@

59

package.json
{
"name": "surrial",
"version": "1.0.0",
"version": "2.0.0",
"description": "Serialize anything. This is surreal!",

@@ -8,11 +8,14 @@ "main": "src/index.js",

"scripts": {
"all": "npm run lint && npm run clean && npm run build && npm run test && npm run stryker",
"clean": "rimraf \"+(src|test)/**/+(*.map|*.js|*.d.ts)\"",
"prebuild": "npm run clean",
"lint": "npm run lint:json && npm run lint:ts",
"lint:json": "prettier --check .eslintrc *.json",
"lint:ts": "eslint --ext .js,.ts --ignore-path .gitignore .",
"build": "tsc",
"postbuild": "tslint --project .",
"pretest": "npm run build",
"test": "nyc --check-coverage --reporter=html --report-dir=reports/coverage --lines 80 --functions 80 --branches 75 mocha \"test/**/*.js\"",
"preversion": "npm t",
"start": "tsc -w",
"test": "nyc --check-coverage --reporter=html --report-dir=reports/coverage --lines 80 --functions 80 --branches 75 mocha --require source-map-support/register \"test/**/*.js\"",
"stryker": "stryker run",
"preversion": "npm run all",
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md",
"postversion": "npm publish && git push && git push --tags",
"postversion": "git push && git push --tags",
"release:patch": "npm version patch -m \"chore(release): %s\"",

@@ -38,22 +41,28 @@ "release:minor": "npm version minor -m \"chore(release): %s\"",

"author": "Nico Jansen",
"license": "Apache2",
"license": "Apache-2.0",
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",
"@types/node": "^11.9.0",
"@stryker-mutator/core": "^2.5.0",
"@stryker-mutator/html-reporter": "^2.5.0",
"@stryker-mutator/mocha-framework": "^2.5.0",
"@stryker-mutator/mocha-runner": "^2.5.0",
"@stryker-mutator/typescript": "^2.5.0",
"@types/chai": "^4.2.9",
"@types/mocha": "^7.0.1",
"@types/node": "^13.7.1",
"@typescript-eslint/eslint-plugin": "^2.19.2",
"@typescript-eslint/parser": "^2.19.2",
"chai": "^4.2.0",
"conventional-changelog-cli": "^2.0.11",
"mocha": "^5.2.0",
"nyc": "^13.2.0",
"rimraf": "^2.6.3",
"source-map-support": "^0.5.10",
"stryker": "^0.35.0",
"stryker-api": "^0.24.0",
"stryker-html-reporter": "^0.18.0",
"stryker-mocha-framework": "^0.15.0",
"stryker-mocha-runner": "^0.17.0",
"stryker-typescript": "^0.18.0",
"tslint": "^5.12.1",
"typescript": "^3.3.3"
}
"conventional-changelog-cli": "^2.0.31",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-prettier": "^3.1.2",
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"prettier": "^1.19.1",
"rimraf": "^3.0.2",
"source-map-support": "^0.5.16",
"typescript": "^3.7.5"
},
"dependencies": {}
}

@@ -21,3 +21,3 @@ [![Build Status](https://travis-ci.org/nicojs/node-surrial.svg?branch=master)](https://travis-ci.org/nicojs/node-surrial)

```javascript
const { serialize, deserialize } = require('surrial');
const { serialize, deserialize, surrial } = require('surrial');

@@ -69,3 +69,31 @@ class Person {

```
An example of the `surrial` tag for template literals:
```js
const decade = [new Date(2010, 1, 1), new Date(2020, 1, 1)];
surrial`new Set(${decade})`;
// => 'new Set([new Date("2010-01-31T23:00:00.000Z"),new Date("2020-01-31T23:00:00.000Z")])'
```
You can customize the output string using the `surrialize()` method (comparable to the [`toJSON`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON_behavior) method for `JSON.stringify`).
```ts
// A typescript example
class Person implements Surrializable {
public age: number;
constructor(ageInMonths: number) {
this.age = Math.floor(ageInMonths / 12);
}
surrialize() {
return surrial`new Person(${this.age * 12})`;
}
}
const input = new Person(25);
const actual = serialize(input, [Person]);
const output = deserialize(actual, [Person]);
expect(output).instanceOf(Person);
expect(output).deep.eq(input);
```
## Api

@@ -77,2 +105,9 @@

/**
* A surrial template tag, useful for building templates strings while enforcing the values to be serialized using surrial.
* @param templateLiterals The template literals
* @param values The values to be serialized using surrial
*/
export function surrial(templateLiterals: TemplateStringsArray, ...values: unknown[]) {
/**
* Serializes the thing to a javascript string. This is NOT necessarily a JSON string, but will be valid javascript.

@@ -82,3 +117,3 @@ * @param thing The thing to be serialized

*/
function serialize(thing: any, knownClasses: ClassConstructor[] = []): string {
export function serialize(thing: any, knownClasses: ClassConstructor[] = []): string {

@@ -92,3 +127,3 @@

*/
function deserialize(serializedThing: string, knownClasses: ClassConstructor[] = []): any;
export function deserialize(serializedThing: string, knownClasses: ClassConstructor[] = []): any;
```

@@ -108,2 +143,4 @@

* Deserialize using a `deserialize` convenience method. This uses the `new Function(/*...*/)` (comparable to `eval`) (see [limitations](#deserializing-is-no-security-feature-you-will-get-hacked)).
* Serialize values in a template with a handy `surrial` tagged template literal.
* Allow a custom serialize function using `surrialize`.

@@ -110,0 +147,0 @@ ## Limitations

@@ -1,3 +0,5 @@

import ClassConstructor from './ClassConstructor';
export declare function isInstanceOf(thing: any, whitelist: ReadonlyArray<ClassConstructor>): boolean;
import ClassConstructor from './class-constructor';
import { Surrializable } from './surrializable';
export declare function isInstanceOf(thing: unknown, whitelist: ReadonlyArray<ClassConstructor>): boolean;
export declare function isSurrializable(thing: object): thing is Surrializable;
export declare function getParamList(constructor: ClassConstructor): string[];

@@ -7,2 +7,7 @@ "use strict";

exports.isInstanceOf = isInstanceOf;
var SURRIALIZABLE_FN_NAME = 'surrialize';
function isSurrializable(thing) {
return thing && typeof thing[SURRIALIZABLE_FN_NAME] === 'function';
}
exports.isSurrializable = isSurrializable;
function isEcmaScriptClass(constructor) {

@@ -12,8 +17,6 @@ return constructor.toString().startsWith('class');

function getParamList(constructor) {
var splitParams = function (params) {
return params.split(',').map(function (param) { return param.trim(); });
};
var splitParams = function (params) { return params.split(',').map(function (param) { return param.trim(); }); };
var constructorString = constructor.toString();
if (isEcmaScriptClass(constructor)) {
var parametersMatch = constructorString.match(/constructor[^(]*\(([^)]*)\)/);
var parametersMatch = /constructor[^(]*\(([^)]*)\)/.exec(constructorString);
if (parametersMatch) {

@@ -28,3 +31,3 @@ return splitParams(parametersMatch[1]);

else {
var parametersMatch = constructorString.match(/function[^(]*\(([^)]*)\)/);
var parametersMatch = /function[^(]*\(([^)]*)\)/.exec(constructorString);
if (parametersMatch) {

@@ -31,0 +34,0 @@ return splitParams(parametersMatch[1]);

@@ -1,3 +0,10 @@

import ClassConstructor from './ClassConstructor';
import ClassConstructor from './class-constructor';
export * from './surrializable';
/**
* A surrial template tag, useful for building templates strings while enforcing the values to be serialized using surrial.
* @param templateLiterals The template literals
* @param values The values to be serialized using surrial
*/
export declare function surrial(templateLiterals: TemplateStringsArray, ...values: unknown[]): string;
/**
* Deserializes a string into it's javascript equivalent. CAUTION! Evaluates the string in the current javascript engine

@@ -4,0 +11,0 @@ * (`eval` or one of its friends). Be sure the `serializedThing` comes from a trusted source!

"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -10,2 +17,21 @@ var helpers_1 = require("./helpers");

/**
* A surrial template tag, useful for building templates strings while enforcing the values to be serialized using surrial.
* @param templateLiterals The template literals
* @param values The values to be serialized using surrial
*/
function surrial(templateLiterals) {
var values = [];
for (var _i = 1; _i < arguments.length; _i++) {
values[_i - 1] = arguments[_i];
}
var stringBuilder = [];
for (var i = 0; i < values.length; i++) {
stringBuilder.push(templateLiterals[i]);
stringBuilder.push(serialize(values[i]));
}
stringBuilder.push(templateLiterals[templateLiterals.length - 1]);
return stringBuilder.join('');
}
exports.surrial = surrial;
/**
* Deserializes a string into it's javascript equivalent. CAUTION! Evaluates the string in the current javascript engine

@@ -18,4 +44,4 @@ * (`eval` or one of its friends). Be sure the `serializedThing` comes from a trusted source!

if (knownClasses === void 0) { knownClasses = []; }
var evalFn = new (Function.bind.apply(Function, [void 0].concat(knownClasses.map(function (t) { return t.name; }), ["\"use strict\";" + os_1.EOL + "return (" + serializedThing + ");"])))();
return evalFn.apply(null, knownClasses);
var evalFn = new (Function.bind.apply(Function, __spreadArrays([void 0], knownClasses.map(function (t) { return t.name; }), ["\"use strict\";" + os_1.EOL + "return (" + serializedThing + ");"])))();
return evalFn.call.apply(evalFn, __spreadArrays([null], knownClasses));
}

@@ -51,2 +77,5 @@ exports.deserialize = deserialize;

}
else if (helpers_1.isSurrializable(thing)) {
return thing.surrialize();
}
else if (helpers_1.isInstanceOf(thing, knownClasses)) {

@@ -71,3 +100,3 @@ return serializeClassInstance(thing, knownClasses);

var origValue = this[key];
if ((origValue !== thing && (helpers_1.isInstanceOf(origValue, BUILD_IN_SUPPORTED_CLASSES) || helpers_1.isInstanceOf(origValue, knownClasses)))) {
if (origValue !== thing && (helpers_1.isInstanceOf(origValue, BUILD_IN_SUPPORTED_CLASSES) || helpers_1.isInstanceOf(origValue, knownClasses))) {
return "@__" + UID + "-" + (escapedValues.push(origValue) - 1) + "__@";

@@ -74,0 +103,0 @@ }

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc