New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jslt

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jslt - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

34

jslt.js

@@ -18,3 +18,3 @@

if (lastError) {
var ex = lastError.stack.reverse().join(".");
var ex = lastError.stack.reverse().join(".") + " - " + lastError.message;
lastError = null;

@@ -62,4 +62,5 @@ throw ex;

const name2 = name.replace(/\\(\.|\[|\])/g, (str, p1) => String.fromCodePoint(p1.codePointAt(0) + 0xE000));
const parts = name2.split("."), hasSpecialChars = name != name2;
const [, nameWithoutType, maybe, type ] = /(.*?)(?::(\?)?(string|number|boolean|array|object|date))?$/.exec(name);
const nameWithSpecialChars = nameWithoutType.replace(/\\(\.|\[|\])/g, (str, p1) => String.fromCodePoint(p1.codePointAt(0) + 0xE000));
const parts = nameWithSpecialChars.split("."), hasSpecialChars = name != nameWithSpecialChars;

@@ -74,3 +75,3 @@ for (var i = 0; i < parts.length; ++i) {

scope = scope[propName];
if (!scope) return scope;
if (!scope) break;

@@ -83,2 +84,7 @@ if (reRes && reRes[2]) {

}
if (type) {
if (scope === undefined) return maybe ? undefined : error(`{{${nameWithoutType}}}`, "Missing required value");
return verifyType(nameWithoutType, type, scope);
}
return scope;

@@ -134,2 +140,22 @@ }

function verifyType(propName, type, value) {
var actualType = typeof value;
if (actualType === "object") {
if (value === null) actualType = "null";
else if (value instanceof Array) actualType = "array";
else if (value instanceof Date) actualType = "date";
}
if (type == actualType) return value;
if (type == "string" && actualType == "number") return String(value);
if (type == "number" && actualType == "string" && !isNaN(Number(value))) return Number(value);
if (type == "date" && actualType == "string") {
let date = new Date(value);
if (date.toJSON() === value) return date;
}
error(`{{${propName}}}`, `Expected ${type}, but received ${actualType}`);
return null;
}
const QueryOperators = {

@@ -136,0 +162,0 @@ $eq(expected, actual) {

2

package.json
{
"name": "jslt",
"version": "0.1.2",
"version": "0.2.0",
"description": "JSON transformer",

@@ -5,0 +5,0 @@ "main": "jslt.js",

@@ -33,3 +33,7 @@

if (arguments.length == 3) [ data, template, expected ] = [ globalData, data, template ];
var res = jslt.transform(data, template);
try {
var res = jslt.transform(data, template);
} catch(ex) {
res = ex;
}
var success = JSON.stringify(res) == JSON.stringify(expected);

@@ -143,2 +147,22 @@ success ? ++passed : ++failed;

console.log(`\nPassed: ${passed} Failed: ${failed}`);
test("type - number - pass", { prop : 3 }, { p : "{{prop:number}}" }, { p : 3 });
test("type - number/string - pass", { prop : "3" }, { p : "{{prop:number}}" }, { p : 3 });
test("type - number/string - fail", { prop : "aa" }, { p : "{{prop:number}}" }, "p.{{prop}} - Expected number, but received string");
test("type - number - fail", { prop : "test" }, { p : "{{prop:number}}" }, "p.{{prop}} - Expected number, but received string");
test("type - boolean - pass", { prop : true }, { p : "{{prop:boolean}}" }, { p : true });
test("type - boolean - fail", { prop : 4 }, { p : "{{prop:boolean}}" }, "p.{{prop}} - Expected boolean, but received number");
test("type - string - pass", { prop : "aa" }, { p : "{{prop:string}}" }, { p : "aa" });
test("type - string/number - pass", { prop : 4 }, { p : "{{prop:string}}" }, { p : "4" });
test("type - string - fail", { prop : true }, { p : "{{prop:string}}" }, "p.{{prop}} - Expected string, but received boolean");
test("type - array - pass", { prop : [] }, { p : "{{prop:array}}" }, { p : [] });
test("type - array - fail", { prop : false }, { p : "{{prop:array}}" }, "p.{{prop}} - Expected array, but received boolean");
test("type - object - pass", { prop : {} }, { p : "{{prop:object}}" }, { p : {} });
test("type - object - fail", { prop : "zz" }, { p : "{{prop:object}}" }, "p.{{prop}} - Expected object, but received string");
test("type - date - pass", { prop : "2015-07-15T22:00:00.000Z" }, { p : "{{prop:date}}" }, { p : "2015-07-15T22:00:00.000Z" });
test("type - date obj - pass", { prop : new Date("2015-07-15T22:00:00.000Z") }, { p : "{{prop:date}}" }, { p : "2015-07-15T22:00:00.000Z" });
test("type - date - fail", { prop : "zz" }, { p : "{{prop:date}}" }, "p.{{prop}} - Expected date, but received string");
test("type - required - fail", { prop1 : "test" }, { p : "{{prop:number}}" }, "p.{{prop}} - Missing required value");
test("type - optional - pass", { prop1 : "test" }, { p : "{{prop:?number}}", p2 : 3 }, { p2 : 3 });
test("type - in-string - fail", { prop : "aa" }, { p : "test {{prop:number}}" }, "p.{{prop}} - Expected number, but received string");
console.log(`\nPassed: ${passed}, Failed: ${failed}`);
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