Coder Social home page Coder Social logo

null-coalescing.js's Introduction

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'

null-coalescing.js's People

Watchers

James Cloos avatar Purexo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.