
Research
/Security News
9 Malicious NuGet Packages Deliver Time-Delayed Destructive Payloads
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.
@rubiin/js-utils
Advanced tools
JavaScript Utilities
npm i @rubiin/js-utils
yarn add @rubiin/js-utils
Common JavaScript packages and utilities used across my projects.
Checks if the a value is an empty object/collection, has no enumerable properties or is any type that is not considered a collection.
isEmpty([]); // true
isEmpty({}); // true
isEmpty(''); // true
isEmpty([1, 2]); // false
isEmpty({ a: 1, b: 2 }); // false
isEmpty('text'); // false
isEmpty(123); // true - type is not considered a collection
isEmpty(true); // true - type is not considered a collection
Picks the key-value pairs corresponding to the given keys from an object.
pick({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 }
Omits the key-value pairs corresponding to the given keys from an object.
omit({ a: 1, b: '2', c: 3 }, ['b']); // { 'a': 1, 'c': 3 }
Calculates the sum of two or more numbers/arrays.
sumOfAnArray(1, 2, 3, 4); // 10
sumOfAnArray(...[1, 2, 3, 4]); // 10
Returns the memoized (cached) function.
// See the `anagrams` snippet.
const anagramsCached = memoize(anagrams);
anagramsCached('javascript'); // takes a long time
anagramsCached('javascript'); // returns virtually instantly since it's cached
console.log(anagramsCached.cache); // The cached anagrams map
Performs left-to-right function composition.
const add5 = x => x + 5;
const multiply = (x, y) => x * y;
const multiplyAndAdd5 = pipeFunctions(multiply, add5);
multiplyAndAdd5(5, 2); // 15
Replaces the names of multiple object keys with the values provided.
const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 };
renameKeys({ name: 'firstName', job: 'passion' }, obj);
// { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 }
Creates an array of key-value pair arrays from an object.
objectToEntries({ a: 1, b: 2 }); // [ ['a', 1], ['b', 2] ]
Creates a shallow clone of value.
var objects = [{ a: 1 }, { b: 2 }];
var shallow = clone(objects);
console.log(shallow[0] === objects[0]);
Subtract the following duration from 15 June 2017 15:29:20
const result = subtractDate(new Date('2017-01-01),'years',2)
// Mon Sep 1 2015 10:19:50
Calculates the difference between two arrays, without filtering duplicate values.
difference([1, 2, 3, 3], [1, 2, 4]); // [3, 3]
Returns every element that exists in any of the two arrays at least once.
union([1, 2, 3], [4, 3, 2]); // [1, 2, 3, 4]
Checks if gicen string is date
console.log(isDate('not-date'));
// false
console.log(isDate('2019-01-10'));
// true
Groups the elements of an array based on the given function.
groupBy([6.1, 4.2, 6.3], Math.floor); // {4: [4.2], 6: [6.1, 6.3]}
groupBy(['one', 'two', 'three'], 'length'); // {3: ['one', 'two'], 5: ['three']}
Sorts an array of objects, ordered by properties and orders.
const users = [
{ name: 'fred', age: 48 },
{ name: 'barney', age: 36 },
{ name: 'fred', age: 40 },
];
orderBy(users, ['name', 'age'], ['asc', 'desc']);
// [{name: 'barney', age: 36}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]
orderBy(users, ['name', 'age']);
// [{name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]
Generates random mnumber of giben length
console.log(randomNumber(6));
// 195315
Generates random string of giben length
console.log(randomString(6));
// a1t4ry
Get string after a substring
strAfter('pineapple', 'pine'); // apple
Get string before a substring
strBefore('pineapple', 'apple'); // pine
Checks if the a value is not an empty object/collectiom
isNotEmpty([]); // false
isNotEmpty({}); // false
isNotEmpty(''); // false
isNotEmpty([1, 2]); // true
isNotEmpty({ a: 1, b: 2 }); // true
isNotEmpty('text'); // true
Checks if the passed value is an object or not.
isObject([1, 2, 3, 4]); // true
isObject([]); // true
isObject(['Hello!']); // true
isObject({ a: 1 }); // true
isObject({}); // true
isObject(true); // false
Get a number after truncating it from the decimal point. No round off is done
fixedDecimal(3.141525, 3); // 3.141
Get a random string of defined length
generateRandomString(6); // fd84bg
Generate a slug from a string
slugify('i love javascript'); // i-love
capitalizeEveryWord('hello world!'); // 'Hello World!'
window.addEventListener(
'resize',
throttle(function (evt) {
console.log(window.innerWidth);
console.log(window.innerHeight);
}, 250),
); // Will log the window dimensions at most every 250ms
unescapeHTML('<a href="#">Me & you</a>');
// '<a href="#">Me & you</a>'
timeTaken(() => Math.pow(2, 10)); // 1024, (logged): timeTaken: 0.02099609375ms
formatDuration(1001); // '1 second, 1 millisecond'
formatDuration(34325055574);
// '397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds'
template('Hello, {{name}}!', { name: 'world' });
//=> Hello, world!
template('Howdy, {{0}}! {{1}}', ['partner', '🤠']);
//=> Howdy, partner! 🤠
template('foo: "{{foo}}"; bar: "{{bar}}";', { foo: 123 });
//=> foo: "123"; bar: "";
template(
`
Name: {{name.last}}, {{name.first}}
Location: {{address.city}} ({{address.country}})
Hobbies: {{hobbies.0}}, {{hobbies.1}}, {{hobbies.2}}
`,
{
name: {
first: 'Luke',
last: 'Edwards',
},
address: {
city: 'Los Angeles',
country: 'USA',
},
hobbies: ['eat', 'sleep', 'repeat'],
},
);
console.log(encrypt('hello','32 bytes hex key','16 bytes hex iv'))
// p5HX3eMlroLYJPhXr2zARg==
console.log(decrypt('p5HX3eMlroLYJPhXr2zARg==','32 bytes hex key','16 bytes hex iv'))
// hello
Any types of contributions are welcome. Feel free to send pull requests or create issues.
Licensed under The MIT License.
FAQs
Some common utilities functions for everyday backend usage with zero dependencies
The npm package @rubiin/js-utils receives a total of 0 weekly downloads. As such, @rubiin/js-utils popularity was classified as not popular.
We found that @rubiin/js-utils 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.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.