1.0.6 • Published 3 years ago
hasget v1.0.6
hasget
By this library you easy can:
- Check if the specified nested property of object exists as a direct property of object;
- Get the specified nested property of object as a direct property of object;
Installing
Using npm:
$ npm install hasget
Using jsDelivr CDN:
<script src="https://cdn.jsdelivr.net/npm/hasget/dist/hasget.min.js"></script>
Using unpkg CDN:
<script src="https://unpkg.com/hasget/dist/hasget.min.js"></script>
Usage
Import (module):
import { has, get } from 'hasget';
Import (global):
const { has, get } = hasget;
Example:
const obj = { user: { firstname: 'Nik', lastname: null } };
has(obj, 'user'); // true
get(obj, 'user'); // { firstname: 'Nik', lastname: null }
has(obj, 'user.firstname'); // true
get(obj, 'user.firstname'); // 'Nik'
has(obj, 'user.lastname'); // true
get(obj, 'user.lastname'); // null
has(obj, 'user.birthdate'); // false
get(obj, 'user.birthdate'); // undefined
has(obj, 'user.birthdate.year'); // false
get(obj, 'user.birthdate.year'); // undefined