Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@mentoc/utils
Advanced tools
A collection of composable utility functions. Take what you need, and uninstall the library. No more bloated utility libraries. Did I mention, I added a minify step?
I feel like there are bits of functionality that I need to use everyday in my javascript projects. All the functionality I'm looking for isn't there or is inside another npm package. I solved the npm portion by allowing developers the option to extract only specific functions. This makes it so that developers are no longer tied to an NPM repo. Take the functions you need and uninstall the library.
npm install @mentoc/utils
Did you know, this library can export only the functions you need?
Let's say we only want the random_array
and shuffle
functions.
Easy. All we have to do is run this:
npm run pluck rand.random_array rand.shuffle
Now if you do an ls, you will notice that there is now a lib.js
file.
If you look at the contents of lib.js, you'll notice it only contains
two exports (for both of the functions we exported).
This command uses dot notation.
If you just run npm run pluck
, you will be greeted with this
object structure:
{
rand: {
random_array: [Function],
pluck_random: [Function],
shuffle: [Function],
shuffle_for: [Function],
random_hex: [Function]
},
array: { seq: [Function], xtract: [Function] }
}
If you want to export the xtract
function, you would run:
npm run pluck array.xtract
You can use npm run pluck-except <name>...
to pluck everything except
for a few functions.
npm run pluck-except rand.shuffle
This will extract the entire library except for shuffle
const {pluck_random} = require('@mentoc/utils').rand
const data = [1,2,3,4,5,6,7,8,9,10]
console.log(pluck_random(data))
const {random_array} = require('@mentoc/utils').rand
/**
* Generate a random array of 20 elements.
* Each element is between the number 1 and 100
*/
let data_set = random_array(1,100,20)
console.log(data_set)
const {shuffle} = require('@mentoc/utils').rand
const data_set = [1,2,3,4,5,6,7,8,9,10]
console.log(shuffle(data_set))
Maybe you want the algorithm to shuffle more than just one time. For this, you can
use the shuffle_for
function. It accepts a number of rounds as it's second parameter.
const {shuffle_for} = require('@mentoc/utils').rand
const data_set = [1,2,3,4,5,6,7,8,9,10]
/**
* This is equivalent to calling shuffle(data_set) 250 times in a loop
*/
console.log(shuffle_for(data_set,250))
const {xtract} = require('@mentoc/utils').array
/** Grab an element out of a deeply nested structure */
let obj = [
{
user: {
id: 1,
name: "Larry Kenobi",
}
},
{
user: {
id: 2,
name: "Jerry Kenobi",
}
},
{
user: {
id: 3,
name: "John Kenobi",
}
}
]
let larry_schema = '0.user.name'
let john_schema = '2.user.name'
console.log('Example dataset: ',obj)
console.log(xtract(obj,larry_schema)) // prints "Larry Kenobi"
console.log(xtract(obj,john_schema)) // prints "John Kenobi"
const {prune} = require('@mentoc/utils').array
/** Delete an element out of a deeply nested structure */
let obj = [
{
user: {
id: 1,
name: "Larry Kenobi",
}
},
{
user: {
id: 2,
name: "Jerry Kenobi",
}
},
{
user: {
id: 3,
name: "John Kenobi",
}
}
]
console.log(prune(obj,'1.user.name')) // removes name: "Jerry Kenobi" from object
There are times when you simply cannot pass in a dot notation in order to crawl
a deeply nested structure. An object key can be any string value. This complicates
matters and breaks the dot notation API. That is why the following functions accept
either a string
or an array
as the second parameter.
const {xtract} = require('@mentoc/utils').array
const first = "-1-_ yes, @#!_this is a valid key\"\"'"
const second = '_9-1 j#'
const weird_object = {
"-1-_ yes, @#!_this is a valid key\"\"'": [
{
'z -1 +4': {
id: 1,
name: 'john doe'
},
'_9-1 j#': {
id: 2,
name: 'jane doe',
},
}
]
}
/**
* Notice how we are passing in an array as opposed to a string for the
* second parameter here.
*/
let jane_doe = xtract(weird_object,[first,0,second,'name'])
console.log(jane_doe) // will print "jane doe"
const {prune} = require('@mentoc/utils').array
const first = "-1-_ yes, @#!_this is a valid key\"\"'"
const second = '_9-1 j#'
const weird_object = {
"-1-_ yes, @#!_this is a valid key\"\"'": [
{
'z -1 +4': {
id: 1,
name: 'john doe'
},
'_9-1 j#': {
id: 2,
name: 'jane doe',
},
}
]
}
/**
* Notice how we are passing in an array as opposed to a string for the
* second parameter here.
*/
let pruned = prune(weird_object,[first,0,second,'name'])
console.log('removed the name of the object with id 2: ',pruned[first][0])
// output of above console.log:
// removed the name of the object with id 2: { 'z -1 +4': { id: 1, name: 'john doe' }, '_9-1 j#': { id: 2 } }
const {seq} = require('@mentoc/utils').array
/**
* The next line creates: [1,2,3,4,5,6,7]
*/
const data_set = seq(1,7)
const {random_hex} = require('@mentoc/utils').rand
const length = 16
console.log(random_hex(16)) // Prints out 16 hex chars (without prepending zeroes)
As stated above, you can export only the functions you need. This is all done for you through the pluck npm commands. Here are the available commands:
lib.js
npm run pluck category.name... [category.name-N]
npm run pluck rand.shuffle rand.rand_between
npm run pluck-except category.name... [category.name-N]
npm run pluck-except rand.shuffle # exports the whole library except for rand.shuffle
pluck
only this time use the minified versions.pluck-except
only this time use the minified versions.lib.js
, run this command to minify it into lib.min.js
export
, only this time minify the results.npm run test
FAQs
A library of useful functions.
The npm package @mentoc/utils receives a total of 1 weekly downloads. As such, @mentoc/utils popularity was classified as not popular.
We found that @mentoc/utils demonstrated a healthy version release cadence and project activity because the last version was released less than 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
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.