New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

observe-object

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

observe-object

Observe JavaScript objects with native Proxy API.

latest
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Observe Object

Observe JavaScript objects with native Proxy API.

Features

  • No messy APIs, just observe(anyObject) and Happy Watching.

  • Support all intrinsic array functions, such as pop(), splice(), arr.length=0

Usage & Cases

// Original object
const obj = { a: 1 };

// Just observe it by
const observed = observe(obj, (obContext, property, value) => {/*...*/});

// Case1. Add new heavy-weight property
observed.b = {
  c: {
    d: [{
      e: 100
    }]
  }
};
======= Property Change =======
HostObject { a: 1 }
Property 'b'
PropertyPath '["b"]'
OldValue null
NewValue { c: { d: [ { e: 100 } ] } }

// Case2. Push new elements into a deep array
observed.b.c.d.push(1, 2);
======= Property Change =======
HostObject { a: 1, b: { c: { d: [ { e: 100 } ] } } }
Property '1'
PropertyPath '["b"]["c"]["d"]["1"]'
OldValue null
NewValue 1
======= Property Change =======
HostObject { a: 1, b: { c: { d: [ { e: 100 }, 1 ] } } }
Property '2'
PropertyPath '["b"]["c"]["d"]["2"]'
OldValue null
NewValue 2
======= Property Change =======
HostObject { a: 1, b: { c: { d: [ { e: 100 }, 1, 2 ] } } }
Property 'length'
PropertyPath '["b"]["c"]["d"]["length"]'
OldValue 3
NewValue 3

// Case3. Modify deep things
observed.b.c.d[0].e = -1;
======= Property Change =======
HostObject { a: 1, b: { c: { d: [ { e: 100 }, 1, 2 ] } } }
Property 'e'
PropertyPath '["b"]["c"]["d"]["0"]["e"]'
OldValue 100
NewValue -1

// Original object
console.log(obj);
{ a: 1, b: { c: { d: [Array] } } } { depth: null }

// Observed object
console.log(observed);
{ a: 1, b: { c: { d: [ { e: -1 }, 1, 2 ] } } }

Keywords

Observe

FAQs

Package last updated on 08 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts