GithubHelp home page GithubHelp logo

micheleriva / mjn Goto Github PK

View Code? Open in Web Editor NEW
69.0 4.0 4.0 6.57 MB

⚡️Like loadash.get, but in ~200 bytes

License: MIT License

TypeScript 83.56% JavaScript 16.44%
undefined-behavior javascript js typescript error-handling objects exception-handling vanilla-js lodash-get

mjn's Introduction

Simple utility to check if a key or a value exists in an object.

Build Status Coverage Maintainability npm
Blazing Fast


Sponsors



No cannot get property x of undefined. Just returns void 0 (undefined) if a value or a key does not exist. Highly inspired from Java's Optional Type.

Index

Installation

yarn

yarn add mjn

npm

npm install --save mjn

cdn

<script src="https://cdn.jsdelivr.net/npm/mjn@latest/dist/dist.min.js"></script>

Usage

Simple Example

import maybe from "mjn"; // 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: "[email protected]",
      phone: "000 0000000"
    },
    languages: ["english", "italian"]
  }
};

const a = maybe(myObject, "user.name");
const b = maybe(myObject, "user.languages[1]");
const c = maybe(myObject, "foo.bar.baz");
const d = maybe(myObject, "foo.bar.baz", "no value!");
const e = maybe(myObject, "foo.bar.baz", () => "I can be a function!");


console.log(a); // => John
console.log(b); // => italian
console.log(c); // => won't log anything!
console.log(d); // => "no value!"
console.log(e); // => "I can be a function!"

Real World React Example

import React from "react";
import ReactDOM from "react-dom";
import maybe from "mjn";

const user = {
  name: {
    first_name: "John",
    last_name: "Doe"
  },
  contacts: {
    phone: "+00 000 0000000",
    email: "[email protected]"
  }
};

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

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

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

Demos

React.js

React Example Edit l71m022x99

Vue.js

Vue Example Edit Vue Template

Vanilla JS

Vanilla JS Vanilla JS

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Michele Riva

💻 💼

Paolo Roth

💻

Mattia Astorino

🎨

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT

mjn's People

Contributors

adamlacombe avatar allcontributors[bot] avatar micheleriva avatar octod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mjn's Issues

[feature-request] Fallback value

Considering this example

maybe(user, "contacts.phone.office") || "You don't have an office phone."

It would be really nice to provide a fallback instead of coding the OR manually, like

maybe(user, "contacts.phone.office", "You don't have an office phone.")

Need a logo!

Is there someone who wants to make a beautiful logo for this project? 😄

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.