Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gawk

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gawk

Observable JavaScript object model

  • 4.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
88
increased by388.89%
Maintainers
1
Weekly downloads
 
Created
Source

gawk

NPM Version NPM Downloads Travis CI Build Test Coverage Deps Dev Deps

Gawk wraps JavaScript objects and arrays making them observable. Once a JavaScript object is gawked, you can listen for changes including deeply nested changes. Only objects and arrays can be gawked. All other types are passed through.

Gawked objects and arrays can be interacted with as if they were regular objects/arrays. Built-in functions such as JSON.stringify() work as expected.

Note: gawk uses ES2015 proxies and thus requires Node.js 6 or newer.

Installation

npm install gawk

Examples

import gawk from 'gawk';

const obj = gawk({
    foo: 'bar'
});

gawk.watch(obj, (obj, src) => {
    console.info('object changed!');
    console.info('new value =', obj);
});

obj.foo = 'baz';

console.info(obj); // { foo: 'baz' }

You can also be notified if a deep child is changed:

const obj = gawk({
    foo: {
        bar: ['a', 'b']
    }
});

gawk.watch(obj, (obj, src) => {
    console.info('object changed!');
    console.info('new value =', obj);
});

obj.foo.bar.push('c', 'd');

console.info(obj); // { foo: { bar: ['a', 'b', 'c', 'd'] } }

To filter watch notifications, simply pass in the property name or array of property names used to filter the gawk object.

const obj = gawk({
	foo: {
		bar: 'hello'
	}
});

gawk.watch(obj, [ 'foo', 'bar' ], value => {
	console.info(`bar changed to ${value}`);
});

obj.foo.bar = 'world!';

To stop watching, simply call gawk.unwatch() with the original listener function.

const obj = gawk({ /* ... */ });

function onChange(obj, src) {
	console.log('changed!');
}

gawk.watch(obj, onChange);

obj.foo = 'bar';

gawk.unwatch(obj, onChange);

obj.foo = 'baz'; // does not fire onChange()

Upgrading to v4

Gawk v4 has dropped all gawk data types. You must always call gawk().

Change all new GawkObject() calls to gawk({}) and new GawkArray() to gawk([]).

Since Gawk v3 and newer uses ES6 Proxies, you no longer need to call obj.get(), obj.set(), obj.delete(), etc.

Methods obj.watch(), obj.merge(), and obj.mergeDeep() have moved to gawk.watch(), gawk.merge(), and gawk.mergeDeep(). The first argument must be a gawk object.

Starting in v3, Gawk no longer hashes values. This means speed. Gawk v3+ is about 19 times faster than v1 and v2.

License

(The MIT License)

Copyright (c) 2016-2017 Chris Barber

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 08 Apr 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc