Giter Club home page Giter Club logo

object-packer's Introduction

Object packer

a helpers for pack and unpack JS-objects to/from base64-string.

Installation

npm install --save object-packer

Usage

Packing/unpacking

import { packable, pack, unpack } from 'object-packer';

class TestClass {
    @packable.flag
    flagField = false;

    @packable.integer(999) // specify max value
    uintField = 0;

    constructor(a = false, b = 0) {
        this.flagField = a;
        this.uintField = b;
    }

    dump() {
        return {
            flagField: this.flagField,
            uintField: this.uintField,
        }
    }
}

pack(new TestClass()); // ["AMiM", {}]
pack(new TestClass(true, 0)); // ["AciM", {}]
pack(new TestClass(false, 42)); // ["VciM", {}]
pack(new TestClass(true, 42)); // ["VMiM", {}]

unpack(TestClass, 'VMiM', {}).dump(); // { flagField: false, uintField: 42 }

Out-of-the-box it contains decorators just for flags and unsigned integers, but you can define your own packer:

const customPacker = {
    reader(instance, index) {
        if (index === 0) return null;
        return instance.collection[index - 1];
    },

    writer(instance, value) {
        return instance.collection.indexOf(value) + 1;
    },
};

class TestClass {
    @packable.custom(4, customPacker)
    itemFromCollection = null;

    collection = ['foo', 'bar'];

    constructor(value) {
        this.itemFromCollection = value;
    }
}

pack(new TestClass()); // ["kBm5", {}]
pack(new TestClass('foo')); // ["kRm5", {}]
pack(new TestClass('bar')); // ["khm5", {}]

By default pack doesn't check unpacked object and it can be unequal to the original.

class AnotherClass {
    @packable.custom(4, customPacker)
    itemFromCollection = null;

    collection = ['new item', 'foo', 'bar'];
}

unpack(AnotherClass, 'kRm5', {}).itemFromCollection // 'new item'

For preventing these situations you can specify a function with will be used for signing base64-string:

@packable(obj => ({ item: obj.itemFromCollection }))
class TestClass {
    @packable.custom(4, customPacker)
    itemFromCollection = 'foo'; // first element from collection

    collection = ['foo', 'bar'];
}

@packable(obj => ({ item: obj.itemFromCollection }))
class AnotherClass {
    @packable.custom(4, customPacker)
    itemFromCollection = 'new item'; // also first element from collection

    collection = ['new item', 'foo', 'bar'];
}

pack(new TestClass()); // ["Qfqy", {}]
pack(new AnotherClass()); // ["YRDP", {}]

You can also mark some fields as an external and they would be extracted to separete object.

class TestClass {
    @packable.external
    name = null;

    constructor(name) {
        this.name = name;
    }
}

pack(new TestClass('long string')); // ["mZFL", {"name": "long string"}]
unpack(TestClass, 'mZFL', {'name': 'long string'}).name; // "long string"

Change tracking

import { packable, track } from 'object-packer';

class TestClass {
    @packable.flag
    flag = false;

    @packable.external
    name = 'string'
}

const onChange = (packedConfig, externalFields) => {
    $state.go('server.configurator', {
    	...externalFields,
        config: packedConfig,
    }, { inherit: true, notify: false, location: 'replace' });
};


const instance = new TestClass();
instance::track(onChange);

instance.flag = true; // onChange will be fired here with arguments 'MiOX' and {name: 'string'}
instance.name = 'new name'; // and fired again with 'MiOX' and {name: 'new name'}

object-packer's People

Contributors

anber avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.