spyo

    1.0.1 • Public • Published

    Spyo

    Observe an object, check when it's change.

    Installation

    Node.js

    npm install spyo --save
    

    Browser

    Local

    <script src="node_modules/spyo/dist/spyo.min.js"></script>

    CDN unpkg

    <script src="https://unpkg.com/spyo/dist/spyo.min.js"></script>

    Example

    const Spyo = require('spyo');
     
    const myObject = {
        firstName: 'Mike',
        lastName: 'Red'
    };
     
    const mySpy = new Spyo(myObject);
     
    myObject.firstName = 'John';
     
    mySpy.onChange((different) => {
        console.log('is different:', different);
    });

    Use provider function

    const mySpy = new Spyo(() => {
        return Math.random();
    });
     
    mySpy.onChange((different) => {
        console.log('is different:', different);
    });

    API

    Spyo

    Kind: global class

    new Spyo(obj, [opts])

    Create instance

    ParamTypeDefaultDescription
    objObject | function

    object that you want watch

    [opts]Object

    configuration object

    [opts.autoWatch]booleantrue

    auto watch

    [opts.checkMs]number50

    interval in milliseconds for every check

    [opts.provider]function

    optional function called on every check that returns new state

    [opts.exclude]String | Array

    exclude a property or more from check

    [opts.autoReset]booleanfalse

    reset changes detected after check

    spyo.refresh(obj)

    Refresh data source object

    Kind: instance method of Spyo

    Param
    obj

    spyo.check() ⇒ Spyo

    Check if it's different and call onChange callback

    Kind: instance method of Spyo

    spyo.watch() ⇒ Spyo

    Start watching

    Kind: instance method of Spyo

    spyo.unwatch([reset]) ⇒ Spyo

    Stop watching

    Kind: instance method of Spyo

    ParamTypeDescription
    [reset]boolean

    reset changes detected

    spyo.isWatching() ⇒ boolean

    Detect if check is active

    Kind: instance method of Spyo

    spyo.onChange(callback) ⇒ Spyo

    Fired when object is isChanged

    Kind: instance method of Spyo

    Param
    callback

    spyo.isChanged() ⇒ boolean

    Check if object is changed

    Kind: instance method of Spyo

    spyo.reset() ⇒ Spyo

    Reset changes detected

    Kind: instance method of Spyo

    Spyo.isEqual(a, b, exclude) ⇒ boolean

    Check if two object are equals (deep check)

    Kind: static method of Spyo

    ParamTypeDescription
    aobject

    first object

    bobject

    second object

    excludeArray

    exclude properties from check

    Spyo.isIterable(obj) ⇒ boolean

    Check if is an array or an plain object

    Kind: static method of Spyo

    Param
    obj

    Spyo.isObject(obj) ⇒ boolean

    Check for plain object

    Kind: static method of Spyo

    ParamType
    obj*

    Spyo.isArray(obj) ⇒ boolean

    Check for array

    Kind: static method of Spyo

    ParamType
    obj*

    Changelog

    You can view the changelog here

    License

    Spyo is open-sourced software licensed under the MIT license

    Author

    Fabio Ricali

    Install

    npm i spyo

    DownloadsWeekly Downloads

    2

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    145 kB

    Total Files

    15

    Last publish

    Collaborators

    • fabioricali