0.1.0 • Published 7 years ago

epk-nil v0.1.0

Weekly downloads
1
License
MIT
Repository
-
Last release
7 years ago

✌️ Nil

Build Status jest Downloads

No more cannot read property x of undefined wih a simple ~200bytes script!

💪 Live Demo

Open v218xznl3

💻 Installation

$ yarn add epk-nil

or

$ npm install epk-nil --save

then inport it as a module

ES6

import Nil from 'epk-nil'

ES5

const Nil = require('epk-nil')

1991 fan? JsDelivr to the rescue!

<script src="https://cdn.jsdelivr.net/npm/epk-nil@0.1.0/dist/dist.js" type="text/javascript"> </script>

👌 Usage

Vanilla JS Example

import Nil from 'epk-nil' // Or import the library as you wish using npm or CDN script tag!

const myObject = {
  user: {
    name: 'John',
    surname: 'Doe',
    birthday: '1995-01-29',
    contacts: {
      email: 'foo@bar.com',
      phone: '000 0000000'
    },
    languages: ['english', 'italian']
  }
}

const a = Nil(myObject, 'user.name')    // => John
const b = Nil(myObject, 'languages[1]') // => italian
const c = Nil(myObject, 'foo.bar.baz')  // => false

if (a) {
  console.log(a) // => John
}

if (b) {
  console.log(b) // => italian
}

if (c) {
  console.log(c) // => won't log anything!
}

Real world usage (React)

import React from "react";
import ReactDOM from "react-dom";
import Nil from "epk-nil";

const user = {
  name: {
    first_name: "John",
    last_name: "Doe"
  },
  contacts: {
    phone: "+00 000 0000000",
    email: "john@doe.do"
  }
};

const App = () => 

    <div className="App">
      <h1>Hello {Nil(user, "name.first_name")}!</h1>
      <h2> {Nil(user, "contacts.email")} </h2>

      <p>
        {Nil(user, "contacts.phone.office") || "You don't have an office phone."}
      </p>
    </div>


const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

⚖️ License

Nil is released undet the MIT License.

0.1.0

7 years ago

0.0.6

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago