Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
ytech-js-extensions
Advanced tools
Simple prototype extensions which can improve your working with js
Simple js extensions for Array, String, Date, Math and Object
Using npm:
npm install ytech-js-extensions
Using with default import
import "ytech-js-extensions"; //ES6 way for import
// Remove item from array
var arr = [{ id: 1 }, { id: 2 }, { id: 3 }];
var removedItem = arr.remove(function (item) {
return item.id == 2;
});
console.log(arr, removedItem);
// Compare dates without time
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));
// Compare strings with ignoring case
var str1 = "test";
var str2 = "tEsT";
console.log(str1.equal(str2));
Using with partial import
import arrayRemove from "ytech-js-extensions/lib/array/remove";
// Remove item from array
var arr = [{ id: 11 }, { id: 12 }, { id: 13 }];
var removedItem = arrayRemove.call(arr, function (item) {
return item.id == 12;
});
console.log(arr, removedItem);
any - last item from array (undefined if array is empty)
executed item: any - add item if it doesn't exist. The second argument => Null - for primitives, string - search by name, function - predicate like in a .filter()
executed item: any - remove item by predicate for searching or by item (for direct comparing)
new Array - concatinate arrays exlcuding null-arrays => Array.concatNotNull([1,2], null)
Boolean - compare with ignoring case
new String - transform from camelCase: 'myCamelCaseString' to 'My Camel Case String'
new String - split string by spaces and join to camelCase: 'My camel Case string' to 'myCamelCaseString'
new String - set to lower case first char
new String - set to upper case first char
Boolean - compare by values
Boolean - compare without time-values
Date
Date
Date
Date
Date
new Date or same String - try parse Date from String-Date-ISO-Format
-1, 0 or 1 - compare without time-value for sort functions
new Date - create UTC date
new Date - create UTC date
Int - generate random int between values
Float - convert kilometers to miles
Float - convert miles to kilometers
Float - convert degrees to radians
Kilometers: Number - distance between 2 coordinates
Latitude: Float - add distance to latitude
Longitude: Float - add distance to longitude
Boolean - recursively compare 2 values with ignoring null and by setted EqaulOptions
the same object: any - recursively find string values and trying parse to Date by Date.tryParseJSON(str)
the same object: any - remove null, undefined, ''(empty-string) properties by setted options
Compare 2 objects by properties (with using EqualOptions)
import "ytech-js-extensions"; //ES6 way for import
import EqualOptions from "ytech-js-extensions/object/equal/equalOptions.js";
// Compare equals
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)); //expected true
//Compare with options
var options = new EqualOptions();
options.ignoreEmptyArrays = true;
options.ignoreFunctions = false; //here we setted ignoreFunctions to false
options.checkKeysLength = false;
options.showFalseReason = true; //or function(message, v1, v2, key) { bla-bla; return message}
console.log(Object.equal(v1, v2, options), options.falseReason); //expected false and falseReason as string message
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 |
Remove null properties (values) from String, Array or Object (with using RemoveOptions)
import "ytech-js-extensions"; //ES6 way for import
import RemoveOptions from "ytech-js-extensions/object/remove/removeOptions.js";
// Remove without default options
var v = {
id: 1,
arr: [1, null, 2],
arr2: [null, " ", undefined],
arr3: [],
s: " ",
s2: " str ",
};
console.log(Object.removeNulls(v)); //expected { id: 1, arr: [1, 2], s2: 'str' }
//Remove with options
var options = new RemoveOptions();
options.removeEmptyArrays = true;
options.removeNullsFromArrays = false;
options.trimStrings = false; //use 's'.trim()
options.removeEmptyStrings = true;
var v = {
id: 1,
arr: [1, null, 2],
arr2: [null, " ", undefined],
arr3: [],
s: " ",
s2: " str ",
};
console.log(Object.removeNulls(v, options)); //expected { id: 1, arr: [1, 2], s2: 'str' }
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 == '' |
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
// removing prototype extensions otherwise it doesn't work with pdfjs
const arrProto = {};
for (const i in Array.prototype) {
arrProto[i] = Array.prototype[i];
delete Array.prototype[i];
}
// ... do something here
// rollback prototype extensions
for (const i in arrProto) {
Array.prototype[i] = arrProto[i];
}
FAQs
Simple prototype extensions which can improve your working with js
The npm package ytech-js-extensions receives a total of 18 weekly downloads. As such, ytech-js-extensions popularity was classified as not popular.
We found that ytech-js-extensions demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.