ytech-js-extensions
Simple js extensions for Array, String, Date, Math and Object
Features
- Lightweight
- ES5 support
- Available importing of each function (not only prototype assignments which are used by default)
Installing
Using npm:
npm install ytech-js-extensions
Example
Using with default import
import "ytech-js-extensions";
var arr = [{ id: 1 }, { id: 2 }, { id: 3 }];
var removedItem = arr.remove(function (item) {
return item.id == 2;
});
console.log(arr, removedItem);
var date1 = new Date(2018, 1, 1, 12, 23, 16);
var date2 = new Date(2018, 1, 1, 12, 24, 19);
console.log(Date.compareByDate(date1, date2));
var str1 = "test";
var str2 = "tEsT";
console.log(str1.equal(str2));
Using with partial import
import arrayRemove from "ytech-js-extensions/lib/array/remove";
var arr = [{ id: 11 }, { id: 12 }, { id: 13 }];
var removedItem = arrayRemove.call(arr, function (item) {
return item.id == 12;
});
console.log(arr, removedItem);
Array
String
Date
Math
Math.Convert
Math.Coord
Object
Object.equal
Compare 2 objects by properties (with using EqualOptions)
import "ytech-js-extensions";
import EqualOptions from "ytech-js-extensions/object/equal/equalOptions.js";
var v1 = { nested: { id: 1 }, arr: ["s", "d"], dt: new Date() };
var v2 = {
nested: { id: 1 },
arr: ["s", "d"],
dt: new Date(),
fnc: function () {},
};
console.log(Object.equal(v1, v2));
var options = new EqualOptions();
options.ignoreEmptyArrays = true;
options.ignoreFunctions = false;
options.checkKeysLength = false;
options.showFalseReason = true;
console.log(Object.equal(v1, v2, options), options.falseReason);
EqualOptions
Param | Type | Default | Description |
---|
checkKeysLength | Boolean | false | true => restrict comparing by properties: equal({}, {v:null}) === false. True will ignore ignoreFunctions and ignoreEmptyArrays if Object.keys.length are different |
ignoreEmptyArrays | Boolean | true | true => equal({}, {arr:[]}) === true |
ignoreFunctions | Boolean | true | true => equal({fnc:function(){return 's'} }, {fnc:function(){return 'b'} }) === true |
showFalseReason | Boolean or Function(msg,v1,v2,key) | false | true if we need to add to options.falseReason message if equal is false function if we need to use own report-logic |
falseReason | String - output | | will be added message if showFalseReason != true and equal is false |
Object.removeNulls
Remove null properties (values) from String, Array or Object (with using RemoveOptions)
import "ytech-js-extensions";
import RemoveOptions from "ytech-js-extensions/object/remove/removeOptions.js";
var v = {
id: 1,
arr: [1, null, 2],
arr2: [null, " ", undefined],
arr3: [],
s: " ",
s2: " str ",
};
console.log(Object.removeNulls(v));
var options = new RemoveOptions();
options.removeEmptyArrays = true;
options.removeNullsFromArrays = false;
options.trimStrings = false;
options.removeEmptyStrings = true;
var v = {
id: 1,
arr: [1, null, 2],
arr2: [null, " ", undefined],
arr3: [],
s: " ",
s2: " str ",
};
console.log(Object.removeNulls(v, options));
RemoveOptions
Param | Type | Default | Description |
---|
removeEmptyArrays | Boolean | true | true => remove arrays with length === 0 |
removeNullsFromArrays | Boolean | true | true => [1, null, 2] filter to [1, 2] |
trimStrings | Boolean | true | true => use the default string.trim() |
removeEmptyStrings | Boolean | true | true => remove properties, values which has string value == '' |
Troubleshooting
-
Some packages (like html2canvas, pdfjs) can stop working if you have prototype extensions of default types (Array, String, Object etc.). In this case we can
-
include in project only specific functions instead of prototype-assign - see Example OR
-
temporarily remove extensions and assign again later
const arrProto = {};
for (const i in Array.prototype) {
arrProto[i] = Array.prototype[i];
delete Array.prototype[i];
}
for (const i in arrProto) {
Array.prototype[i] = arrProto[i];
}