npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

null-coalescing

v1.0.2

Published

Simulate null-coalescing operator like in php with a function (NodeJS)

Downloads

1

Readme

null-coalescing.js

Simulate null-coalescing operator like in php with a function

Installation

$ npm install --save null-coalescing
$ # or
$ npm install --save git+https://github.com/Purexo/null-coalescing.js.git # if you don't want depend to npm-registery

Usage

const $N = require('null-coalescing')

const bar = {
  anonymous: true
}
const object = {
  foo: {
    bar: 'baz'
  }
}
object[bar] = {
  baz: [
    {name: 'name'}
  ]
}

Signature of $N function :

function $N(object = undefined, evaluatedPath = '', defaultValue = undefined, bind = undefined) {}

Simple use-case :

$N(object, 'foo.bar') // 'baz'
// and with dirty path string
$N(object, '  foo.bar ') // 'baz' (It work)

$N(object, `['foo'].bar.length`) // 3

Use-case with default value :

$N(object, 'bar.foo', 'Oh No, a savage default value !!!') // 'Oh No, a savage default value !!!'

its roughly equivalent to object.bar.foo || 'Oh No, a savage default value !!!'
but without TypeError breaking your code ^^
it's more like

var value = 'Oh No, a savage default value !!!'
try { value = object.bar.foo || value }
catch (e) {}

Yeah value is OK (with so many unclear lines)

Let's magic enter in your heart !

Notice [bar] in 2nd argument and {bar} the last argument or in bind or first arg of call

$N(object, `[bar].baz.name`, 'default', {bar}) // 'name'
$N.bind({bar})(object, `[bar].baz.name`) // 'name'
$N.call({bar}, object, `[bar].baz.name` // 'name'
$N.call({bar}, object, `[bar].baz.name.notExistroperty` // undefined
// object[bar].baz.name || 'default' // but without TypeError throwed in your face <3

In truth there is no magic, just a trick with nodejs vm module, this code use vm.runInContext

NodeJs with module, require, and files are really separated, no possibility to get the context of caller, so you need use the bindArgs or bind this to $N function with needed var of your context you want access with evaluatedPath argument

Don't know why you could use like that, but it's possible

$N() // undefined
$N(undefined, 'foo.bar', 'default') // 'default'