Socket
Socket
Sign inDemoInstall

basic-utils

Package Overview
Dependencies
15
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.7.0 to 1.7.1

39

lib/types.js

@@ -13,2 +13,7 @@ 'use strict';

exports.isError = (value) => {
return value instanceof Error;
};
exports.isNan = (value) => {

@@ -170,1 +175,35 @@

};
exports.isDateString = (value) => {
if (!exports.isString(value)) {
return false;
}
return (new Date(value).toString() !== 'Invalid Date') && (!isNaN(new Date(value)));
};
exports.isRegexString = (value) => {
if (!exports.isString(value)) {
return false;
}
if (~value.indexOf('/')) {
const parts = value.split('/');
let regex = value;
regex = parts[1];
const options = parts[2];
try {
new RegExp(regex, options);
return true;
}
catch (e) {
return false;
}
}
return false;
};

86

lib/utils.js

@@ -8,2 +8,69 @@ 'use strict';

const internals = {};
internals.replacer = (key, value) => {
if (Types.isFunction(value)) {
return value.toString();
}
if (Types.isNumber(Date.parse(value))) {
return new Date(value).toString();
}
if (Types.isRegex(value)) {
return value.toString();
}
if (Types.isNan(value)) {
return 'NaN';
}
if (value === Infinity) {
return 'Infinity';
}
if (value === -Infinity) {
return '-Infinity';
}
return value;
};
internals.reviver = (key, value) => {
// check if string is date first
if (Types.isDateString(value)) {
return new Date(value);
}
if (Types.isString(value)) {
if (value === 'NaN') {
return NaN;
}
if (value === 'Infinity') {
return Infinity;
}
if (value === '-Infinity') {
return -Infinity;
}
if (Types.isRegexString(value)) {
return new RegExp(value);
}
return (value.substring(0, 8) === 'function') ? Eval('(' + value + ')') : value;
}
return value;
};
exports.deepFreeze = DeepFreeze;

@@ -39,14 +106,7 @@

exports.serialize = (obj) => {
exports.serialize = (obj, spacer) => {
// Returns value and converts if it is a function to a string
try {
return JSON.stringify(obj, (key, value) => {
if (Types.isFunction(value)) {
return value.toString();
}
return value;
}, 4);
return JSON.stringify(obj, internals.replacer, 4);
}

@@ -63,9 +123,3 @@ catch (e) {

try {
return JSON.parse(str, (key, value) => {
if (!Types.isString(value)) {
return value;
}
return (value.substring(0, 8) === 'function') ? Eval('(' + value + ')') : value;
});
return JSON.parse(str, internals.reviver);
}

@@ -72,0 +126,0 @@ catch (e) {

6

package.json
{
"name": "basic-utils",
"version": "1.7.0",
"version": "1.7.1",
"description": "basic js utils",

@@ -21,4 +21,4 @@ "main": "lib/index.js",

"code": "^4.0.0",
"coveralls": "^2.11.14",
"lab": "^11.1.0"
"coveralls": "^2.11.15",
"lab": "^11.2.1"
},

@@ -25,0 +25,0 @@ "dependencies": {

@@ -38,2 +38,5 @@ # basic-utils

- isAscii
- isError
- isRegexString
- isDateString

@@ -40,0 +43,0 @@

@@ -27,2 +27,4 @@ 'use strict';

expect(!Utils.isObj({})).to.be.false();
expect(!Utils.isObj([])).to.be.true();
done();

@@ -40,2 +42,4 @@

expect(Utils.isNan(NaN)).to.be.true();
expect(!Utils.isNan(1)).to.be.true();
expect(!Utils.isNan(NaN)).to.be.false();
done();

@@ -53,2 +57,4 @@

expect(!Utils.isArray({})).to.be.true();
expect(!Utils.isArray([])).to.be.false();
done();

@@ -67,2 +73,5 @@

expect(!Utils.isString(null)).to.be.true();
expect(!Utils.isString('string')).to.be.false();
done();

@@ -79,2 +88,4 @@

expect(Utils.isDate()).to.be.false();
expect(!Utils.isDate([])).to.be.true();
expect(!Utils.isDate(new Date())).to.be.false();
done();

@@ -91,2 +102,4 @@

expect(Utils.isRegex()).to.be.false();
expect(!Utils.isRegex([])).to.be.true();
expect(!Utils.isRegex(new RegExp())).to.be.false();
done();

@@ -422,3 +435,69 @@

it('should test isDateString', (done) => {
const date = new Date();
expect(Utils.isDateString(date.toString())).to.be.true();
expect(Utils.isDateString(date.toISOString())).to.be.true();
expect(Utils.isDateString(date.toUTCString())).to.be.true();
expect(Utils.isDateString(date.toGMTString())).to.be.true();
expect(Utils.isDateString(date.toDateString())).to.be.true();
expect(Utils.isDateString('Fri Nov 32 2016')).to.be.false();
expect(Utils.isDateString(new Date('not a date string').toString())).to.be.false();
expect(Utils.isDateString([])).to.be.false();
done();
});
it('should test isRegexString', (done) => {
const regexp = new RegExp();
const macAddress = new RegExp(/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/);
expect(Utils.isRegexString(regexp.toString())).to.be.true();
expect(Utils.isRegexString(macAddress.toString())).to.be.true();
expect(Utils.isRegexString('/test/g')).to.be.true();
expect(Utils.isRegexString('/test/')).to.be.true();
expect(Utils.isRegexString('/test/^([')).to.be.false();
expect(Utils.isRegexString([])).to.be.false();
expect(Utils.isRegexString('test')).to.be.false();
expect(Utils.isRegexString(NaN)).to.be.false();
done();
});
it('should test isError', (done) => {
expect(Utils.isError(new Error())).to.be.true();
expect(Utils.isError(new TypeError())).to.be.true();
expect(Utils.isError(new RangeError())).to.be.true();
expect(Utils.isError(new ReferenceError())).to.be.true();
expect(Utils.isError(new SyntaxError())).to.be.true();
expect(Utils.isError('')).to.be.false();
done();
});
it('should test \'or\' comparisons', (done) => {
const value = { hello: 'world' };
const comparison = (Utils.isObj(value) || Utils.isArray(value));
expect(comparison).to.be.true();
done();
});
it('should test \'or\' not comparisons', (done) => {
const value = 'hello';
const comparison = (!Utils.isObj(value) || !Utils.isArray(value));
expect(comparison).to.be.true();
done();
});
});

@@ -5,7 +5,8 @@ 'use strict';

const Lab = require('lab');
const Utils = require('../lib/index.js');
// Fixtures
const Utils = require('../lib/index.js');
// Set-up lab

@@ -57,9 +58,29 @@ const lab = exports.lab = Lab.script();

};
const obj = {};
obj.a = {
b: obj
const circular = {};
circular.a = {
b: circular
};
const obj = {
date: new Date(),
regexp: new RegExp(),
nan: NaN,
positive: Infinity,
negative: -Infinity,
key: 'value'
};
const string = Utils.serialize(obj);
const parsed = Utils.deserialize(string);
expect(string).to.be.a.string();
console.log(string);
expect(parsed).to.be.an.object();
expect(parsed.date).to.be.a.date();
expect(parsed.regexp).to.be.a.regexp();
expect(parsed.nan).to.equal(NaN);
expect(parsed.positive).to.equal(Infinity);
expect(parsed.negative).to.equal(-Infinity);
expect(Utils.serialize(fn)).to.be.a.string();
expect(Utils.serialize(toString)).to.be.a.string();
expect(Utils.serialize(obj)).to.be.an.instanceof(Error);
expect(Utils.serialize(circular)).to.be.an.instanceof(Error);
done();

@@ -74,3 +95,3 @@ });

expect(Utils.deserialize(deserialize)).to.be.a.function();
expect(Utils.deserialize(date)).to.be.a.string();
expect(Utils.deserialize(date)).to.be.a.date();
expect(Utils.deserialize('[1,2,3]')).to.be.an.array();

@@ -77,0 +98,0 @@ expect(Utils.deserialize(new RegExp())).to.be.an.instanceof(Error);

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc