objeto
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

objeto

install size

🦺 Object helper to get, set, delete and check nested properties without risk.

  • 🚀 Lightweight.
  • ⚪️ Zero dependencies.

Table of contents


Import

const objeto = require("objeto");

Usage

get

If the targetObject doesn't contain the path property, it will return undefined.

  • targetObject object: Object to target.
  • path string | Array<string>: Dot path "prop1.prop2" or array path ["prop1", "prop2"].
objeto(targetObject).get(path);

set

  • targetObject object: See get.
  • path See get.
  • value any: Value to set.
objeto(targetObject).set(path, value);

delete

  • deletedValue any: Deleted value.
  • targetObject object: See get.
  • path See get.
const deletedValue = objeto(targetObject).delete(path);

has

Check if the targetObject has the specified property.

  • targetObject object: See get.
  • path See get.
objeto(targetObject).has(path);

isType

Check if a property value type is equal the specified type.

  • targetObject object: See get.
  • path See get.
  • type string: See typeof.
objeto(targetObject).isType(path, type);

Example

let bike = {
	wheel1: {
		type: "AD-56",
		status: "ok"
	},
	wheel2: {
		type: "AT-77",
		status: "ok"
	},
};

objeto(bike).get("wheel2.type"); // "AT-77"
objeto(bike).set("wheel2.type", "newType");
objeto(bike).delete("wheel2.type"); // "AT-77"
objeto(bike).has("wheel2.type"); // true
objeto(bike).isType("wheel2.type", "string"); // true

objeto(bike).get("wheel2.type.doesntExists"); // undefined
objeto(bike).set("wheel2.type.doesntExists.newProp", "newType"); // new nested property is created
objeto(bike).has("wheel2.type.doesntExists"); // false

Go to top

Package Sidebar

Install

npm i objeto

Weekly Downloads

7

Version

1.0.0

License

ISC

Unpacked Size

6.7 kB

Total Files

4

Last publish

Collaborators

  • icaruk