Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
extant
Advanced tools
Readme
Basically, SQL COALESCE in JavaScript; return the first value that is not null-like.
What | Where |
---|---|
Discussion | https://github.com/bigeasy/extant/issues/1 |
Documentation | https://bigeasy.github.io/extant |
Source | https://github.com/bigeasy/extant |
Issues | https://github.com/bigeasy/extant/issues |
CI | https://travis-ci.org/bigeasy/extant |
Coverage: | https://codecov.io/gh/bigeasy/extant |
License: | MIT |
Extant installs from NPM.
npm install extant
Extant is an implementation of SQL's COALESCE that I've used for some time to
deal with the fact that JavaScript truthiness will treat ''
and 0
as true so
the ||
operator can't always be used to create given or default one-liner.
const { coalesce } = require('extant')
function foo (count) {
count = coalesce(count, 1)
for (let i = 0; i < count; i++) {
console.log('hello')
}
}
foo()
We use the name "extant" on NPM because we want the first extant argument.
README.md
This README.md
is also a unit test using the
Proof unit test framework. We'll use the
Proof okay
function to assert out statements in the readme. A Proof unit test
generally looks like this.
require('proof')(4, async okay => {
okay('always okay')
okay(true, 'okay if true')
okay(1, 1, 'okay if equal')
okay({ value: 1 }, { value: 1 }, 'okay if deep strict equal')
})
You can run this unit test yourself to see the output from the various code sections of the readme.
git clone git@github.com:bigeasy/duplicitous.git
cd duplicitous
npm install --no-package-lock --no-save
node test/readme.t.js
The 'extant'
module exports a single coalesce
function.
const { coalesce } = require('extant')
Note that Extant is SQL's COALESCE
. It returns the first non-null-like value,
that is the first value that is not == null
, which would be null
or
undefined
. If there is no such argument it returns null
.
const maybe = null
let maybeNot
okay(coalesce(maybe, maybeNot, 1), 1, 'return first non-null value')
okay(coalesce(maybe, maybeNot), null, 'return null')
okay(coalesce(), null, 'no arguments is also null')
I've used this module for a long time to initialize function arguments.
function increment (value, increment) {
return value + coalesce(increment, 1)
}
okay(increment(1, 2), 3, 'specify parameter')
okay(increment(1), 2, 'use default parameter')
Although these days I'd probably just use the JavaScript default parameters syntax.
function increment (value, increment = 1) {
return value + increment
}
okay(increment(1, 2), 3, 'specify default parameter')
okay(increment(1), 2, 'use default default parameter')
And when Node.js 14 goes end-of-life I'll switch to ??
, but for now its still
useful when initializing from named parameters when you want to preserve the
original options array. You can always do this for simple cases.
function increment (value, { increment = 1 } = {}) {
return value + increment
}
okay(increment(1, { increment: 2 }), 3, 'specify destructed named parameters')
okay(increment(1), 2, 'use default destructed named parameters')
But, if you wanted keep the options object around for some reason coalesce
still comes in handy.
function increment (value, options = {}) {
return value + coalesce(options.increment, 1)
}
okay(increment(1, { increment: 2 }), 3, 'specify named arguments')
okay(increment(1), 2, 'use default named arguments')
Really would take an example where your constructing an object that has a lot of
options, or nested options. At that point the destructuring gets too verbose and
coalesce
is more appropriate.
Another place where it is useful is variadic functions where destructed and default parameters are not an option.
function adjuster (value, ...vargs) {
const increment = typeof vargs[0] == 'boolean' ? vargs.shift() : true
const by = coalesce(vargs.shift(), 1)
return value + (by * (increment ? 1 : -1))
}
okay(adjuster(1, true, 2), 3, 'increment variadic')
okay(adjuster(1, false), 0, 'decrement variadic')
FAQs
Basically, SQL COALESCE in JavaScript; return the first value that is not null.
The npm package extant receives a total of 139 weekly downloads. As such, extant popularity was classified as not popular.
We found that extant 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.