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

immutable-class-tester

Package Overview
Dependencies
Maintainers
3
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

immutable-class-tester - npm Package Compare versions

Comparing version 0.7.2 to 0.7.3

lib/index.js

124

build/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var deep_equal_1 = tslib_1.__importDefault(require("deep-equal"));
var has_own_prop_1 = tslib_1.__importDefault(require("has-own-prop"));
var PROPERTY_KEYS = [
exports.testImmutableClass = void 0;
const tslib_1 = require("tslib");
const deep_equal_1 = (0, tslib_1.__importDefault)(require("deep-equal"));
const has_own_prop_1 = (0, tslib_1.__importDefault)(require("has-own-prop"));
const PROPERTY_KEYS = [
'name',

@@ -21,33 +22,32 @@ 'defaultValue',

];
function testImmutableClass(ClassFn, objects, options) {
if (options === void 0) { options = {}; }
function testImmutableClass(ClassFn, objects, options = {}) {
if (typeof ClassFn !== 'function')
throw new TypeError("ClassFn must be a constructor function");
throw new TypeError(`ClassFn must be a constructor function`);
if (!Array.isArray(objects) || !objects.length) {
throw new TypeError("objects must be a non-empty array of js to test");
throw new TypeError(`objects must be a non-empty array of js to test`);
}
var newThrows = options.newThrows;
var context = options.context;
var className = ClassFn.name;
const newThrows = options.newThrows;
const context = options.context;
const className = ClassFn.name;
if (className.length < 1)
throw new Error("Class must have a name of at least 1 letter");
var instanceName = className[0].toLowerCase() + className.substring(1);
throw new Error(`Class must have a name of at least 1 letter`);
const instanceName = className[0].toLowerCase() + className.substring(1);
if (typeof ClassFn.fromJS !== 'function')
throw new Error(className + ".fromJS should exist");
var instance = ClassFn.fromJS(objects[0], context);
var objectProto = Object.prototype;
throw new Error(`${className}.fromJS should exist`);
const instance = ClassFn.fromJS(objects[0], context);
const objectProto = Object.prototype;
if (instance.valueOf === objectProto.valueOf) {
throw new Error("Instance should implement valueOf");
throw new Error(`Instance should implement valueOf`);
}
if (instance.toString === objectProto.toString) {
throw new Error("Instance should implement toString");
throw new Error(`Instance should implement toString`);
}
if (typeof instance.toJS !== 'function') {
throw new Error("Instance should have a toJS function");
throw new Error(`Instance should have a toJS function`);
}
if (typeof instance.toJSON !== 'function') {
throw new Error("Instance should have a toJSON function");
throw new Error(`Instance should have a toJSON function`);
}
if (typeof instance.equals !== 'function') {
throw new Error("Instance should have an equals function");
throw new Error(`Instance should have an equals function`);
}

@@ -58,9 +58,9 @@ if (ClassFn.PROPERTIES) {

}
ClassFn.PROPERTIES.forEach(function (property, i) {
ClassFn.PROPERTIES.forEach((property, i) => {
if (typeof property.name !== 'string') {
throw new Error("Property " + i + " is missing a name");
throw new Error(`Property ${i} is missing a name`);
}
Object.keys(property).forEach(function (key) {
Object.keys(property).forEach(key => {
if (!PROPERTY_KEYS.includes(key)) {
throw new Error("PROPERTIES should include " + key);
throw new Error(`PROPERTIES should include ${key}`);
}

@@ -70,36 +70,36 @@ });

}
for (var i = 0; i < objects.length; i++) {
var where = "[in object " + i + "]";
var objectJSON = JSON.stringify(objects[i]);
var objectCopy1 = JSON.parse(objectJSON);
var objectCopy2 = JSON.parse(objectJSON);
var inst = ClassFn.fromJS(objectCopy1, context);
if (!deep_equal_1.default(objectCopy1, objectCopy2)) {
throw new Error(className + ".fromJS function modified its input :-(");
for (let i = 0; i < objects.length; i++) {
const where = `[in object ${i}]`;
const objectJSON = JSON.stringify(objects[i]);
const objectCopy1 = JSON.parse(objectJSON);
const objectCopy2 = JSON.parse(objectJSON);
const inst = ClassFn.fromJS(objectCopy1, context);
if (!(0, deep_equal_1.default)(objectCopy1, objectCopy2)) {
throw new Error(`${className}.fromJS function modified its input :-(`);
}
if (!(inst instanceof ClassFn)) {
throw new Error(className + ".fromJS did not return a " + className + " instance " + where);
throw new Error(`${className}.fromJS did not return a ${className} instance ${where}`);
}
if (typeof inst.toString() !== 'string') {
throw new Error(instanceName + ".toString() must return a string " + where);
throw new Error(`${instanceName}.toString() must return a string ${where}`);
}
if (inst.equals(undefined) !== false) {
throw new Error(instanceName + ".equals(undefined) should be false " + where);
throw new Error(`${instanceName}.equals(undefined) should be false ${where}`);
}
if (inst.equals(null) !== false) {
throw new Error(instanceName + ".equals(null) should be false " + where);
throw new Error(`${instanceName}.equals(null) should be false ${where}`);
}
if (inst.equals([]) !== false) {
throw new Error(instanceName + ".equals([]) should be false " + where);
throw new Error(`${instanceName}.equals([]) should be false ${where}`);
}
if (!deep_equal_1.default(inst.toJS(), objects[i])) {
throw new Error(className + ".fromJS(obj).toJS() was not a fixed point (did not deep equal obj) " + where);
if (!(0, deep_equal_1.default)(inst.toJS(), objects[i])) {
throw new Error(`${className}.fromJS(obj).toJS() was not a fixed point (did not deep equal obj) ${where}`);
}
var instValueOf = inst.valueOf();
const instValueOf = inst.valueOf();
if (inst.equals(instValueOf)) {
throw new Error("inst.equals(inst.valueOf()) " + where);
throw new Error(`inst.equals(inst.valueOf()) ${where}`);
}
var instLazyCopy = {};
for (var key in inst) {
if (!has_own_prop_1.default(inst, key))
const instLazyCopy = {};
for (const key in inst) {
if (!(0, has_own_prop_1.default)(inst, key))
continue;

@@ -109,7 +109,7 @@ instLazyCopy[key] = inst[key];

if (inst.equals(instLazyCopy)) {
throw new Error("inst.equals(*an object with the same values*) " + where);
throw new Error(`inst.equals(*an object with the same values*) ${where}`);
}
if (newThrows) {
var badInst = void 0;
var thrownError = void 0;
let badInst;
let thrownError;
try {

@@ -122,28 +122,28 @@ badInst = new ClassFn(instValueOf);

if (!thrownError || badInst) {
throw new Error("new " + className + " did not throw as indicated " + where);
throw new Error(`new ${className} did not throw as indicated ${where}`);
}
}
else {
var instValueCopy = new ClassFn(instValueOf);
const instValueCopy = new ClassFn(instValueOf);
if (!inst.equals(instValueCopy)) {
throw new Error("new " + className + "().toJS() is not equal to the original " + where);
throw new Error(`new ${className}().toJS() is not equal to the original ${where}`);
}
if (!deep_equal_1.default(instValueCopy.toJS(), inst.toJS())) {
throw new Error("new " + className + "(" + instanceName + ".valueOf()).toJS() returned something bad " + where);
if (!(0, deep_equal_1.default)(instValueCopy.toJS(), inst.toJS())) {
throw new Error(`new ${className}(${instanceName}.valueOf()).toJS() returned something bad ${where}`);
}
}
var instJSONCopy = ClassFn.fromJS(JSON.parse(JSON.stringify(inst)), context);
const instJSONCopy = ClassFn.fromJS(JSON.parse(JSON.stringify(inst)), context);
if (!inst.equals(instJSONCopy)) {
throw new Error("JS Copy does not equal original " + where);
throw new Error(`JS Copy does not equal original ${where}`);
}
if (!deep_equal_1.default(instJSONCopy.toJS(), inst.toJS())) {
throw new Error(className + ".fromJS(JSON.parse(JSON.stringify(" + instanceName + "))).toJS() returned something bad " + where);
if (!(0, deep_equal_1.default)(instJSONCopy.toJS(), inst.toJS())) {
throw new Error(`${className}.fromJS(JSON.parse(JSON.stringify(${instanceName}))).toJS() returned something bad ${where}`);
}
}
for (var j = 0; j < objects.length; j++) {
var objectJ = ClassFn.fromJS(objects[j], context);
for (var k = j; k < objects.length; k++) {
var objectK = ClassFn.fromJS(objects[k], context);
for (let j = 0; j < objects.length; j++) {
const objectJ = ClassFn.fromJS(objects[j], context);
for (let k = j; k < objects.length; k++) {
const objectK = ClassFn.fromJS(objects[k], context);
if (objectJ.equals(objectK) !== Boolean(j === k)) {
throw new Error("Equality of objects " + j + " and " + k + " was wrong");
throw new Error(`Equality of objects ${j} and ${k} was wrong`);
}

@@ -150,0 +150,0 @@ }

{
"name": "immutable-class-tester",
"version": "0.7.2",
"version": "0.7.3",
"description": "A helper for testing immutable classes",

@@ -14,29 +14,17 @@ "keywords": [

},
"license": "MIT",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "http://github.com/implydata/immutable-class-tester.git"
"url": "http://github.com/implydata/immutable-class-tester.git",
"directory": "packages/immutable-class-tester"
},
"main": "build/index.js",
"typings": "build/index.d.ts",
"files": [
"build/"
],
"module": "lib/index.js",
"typings": "types/index.d.ts",
"scripts": {
"compile": "./compile",
"pretest": "npm run compile",
"test": "npm run tslint && jest --silent 2>&1",
"tslint": "./node_modules/.bin/tslint -c tslint.json --project tsconfig.json --formatters-dir ./node_modules/awesome-code-style/formatter 'src/**/*.ts?(x)'",
"tslint-fix": "npm run tslint -- --fix",
"tslint-changed-only": "git diff --diff-filter=ACMR --name-only | grep -E \\.tsx\\?$ | xargs ./node_modules/.bin/tslint -c tslint.json --project tsconfig.json --formatters-dir ./node_modules/awesome-code-style/formatter",
"tslint-fix-changed-only": "npm run tslint-changed-only -- --fix"
"compile": "tsc --module commonjs && tsc --module esnext --outDir lib/",
"prepublishOnly": "npm run compile",
"test": "jest --silent 2>&1",
"watch": "tsc --watch"
},
"prettier": {
"trailingComma": "all",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"printWidth": 100,
"endOfLine": "lf"
},
"jest": {

@@ -54,8 +42,7 @@ "preset": "ts-jest",

"devDependencies": {
"@types/jest": "^24.0.15",
"awesome-code-style": "^1.4.2",
"jest": "^24.8.0",
"ts-jest": "^24.0.2",
"typescript": "^3.5.3"
"@types/jest": "^27.4.0",
"jest": "^27.4.7",
"ts-jest": "^27.1.3",
"typescript": "^4.5.5"
}
}
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