Giter Site home page Giter Site logo

extendable-immutable's Introduction

Wrapper classes around Immutable.js that turn it inheritable

Extendable Immutable.js

About

Ever wished that you could have OrderedMaps, Maps or Lists with extra methods, that make your life easier? .ofCourse()!

Getting Started

Installing the latest version via npm takes just a second:

npm install --save extendable-immutable

Import what you need:

import { OrderedMap } from 'extendable-immutable'

class Collection extends OrderedMap {
// ...

Quick Intro

import { OrderedMap } from 'extendable-immutable'

class Collection extends OrderedMap {
  static isCollection(val) {
    return val && val instanceof Collection;
  }

  doMagic() {
    return this.map(x => x.set("magic", true));
  }
}

const magic = new Collection();

magic instanceof Immutable.OrderedMap; // true
Immutable.OrderedMap.isOrderedMap(magic); // true

extendable-immutable's People

Contributors

greenkeeperio-bot avatar kitten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

extendable-immutable's Issues

Consider replacing `copy` with `Object.setPrototypeOf`

Currently __wrapImmutable is copying over all values of the immutable data structure into a new instance of the current subclass. It could be more performant to just use setPrototypeOf and thus change the value's prototype to the current subclass instead.

TypeError while testing extended Immutable.List.sort() with jest

Hi @philpl ,
thx for your great work on extendable-iummutable, i've liked it so much.

But now i'm faced with weird error while trying to test my extended class with Jest, saying:

TypeError: Class constructor Second cannot be invoked without 'new'

while using immutable.reify

Here is my code:

import { fromJS } from 'immutable'
import { List } from 'extendable-immutable'

const list = fromJS([
  {title: "A"},
  {title: "E"},
  {title: "B"},
  {title: "D"},
  {title: "C"},
])

const result = fromJS([
  {title: "A"},
  {title: "B"},
  {title: "C"},
  {title: "D"},
  {title: "E"},
])

class Second extends List {
  lol(){
    return 'LOL:)'
  }
}

const originalList = new List(list)
const secondList = new Second(list)

describe('original List', () => {
  it('should return sorted list', () => {
    expect(originalList.sort().toJS(), result)
  })
})

describe('extended List', () => {
  it('should return sorted list', () => {
    expect(secondList.sort().toJS(), result)
  })
  it('should say LOL!', () => {
    expect(secondList.lol(), "LOL:)")
  })
})

Could you please help me to deal with it?

cannot create instances of data structures without using new operator

Immutablejs at the time of this post allows for creating immutable instances with factory functions like so:

import { Map } from 'immutable'

const myData = Map()
// Map {}

In extendable-immutable not so much however:

import { Map } from 'extendable-immutable'

const myData = Map()
// TypeError: this.__wrapImmutable is not a function

This however works fine:

import { Map } from 'extendable-immutable'

const myData = new Map()
// Map {}

If this is functionality you're willing merge I'll get going on a pull request :)

Compatibility with Immutable v4

Currently immutablejs developers are preparing a new version of this library (current branch master has version in 4.0.0-rc.2).

Will extendable-immutable they are support the new version of immutablejs?

[Docs] Default values

Hello!

Could you give me/us a example in the docs to do something like this?

const ApiModelRecord = Record(
    {
      status: "init", // "init" | "pending" | "idle" | "errored"
      body: new BasicJsonApiModel(),
    },
    `Api${name}Model`,
  )

in extandable-immutable.js ?

Thank you!

Typescript types addition

Hi, I could not find any typescript definitions for this project so I have had a first attempt at creating them. The definition can be found here.

Nb. This is my first attempt at creating a definition file, but I hope it's of use.

Using Immutable-js Map given functions

Hello !

I was searching for a way to implement my own toString() function on the immutable-js lib when I came around yours ! So thank you very much for this work, I was happy to find it :)

Now I did came here for an issue... I tried extending my class :

import { List, Record } from 'immutable';
import { Map } from 'extendable-immutable';

export default class ParseTree extends Map {
    constructor ( token = "empty", data = {}, children = List() ) {
        super( { token:token, data: ParseTree.initData(token, data), children:children });
    }

Question 1- Can you do a mix of your library and immutable's ? As ParseTree is a Map from you, but it's children is directly from Immutable List. It shouldn't change much to me... But considering the behavior I have after that, I prefer to check.

I then test a few things :

    toString ( ) {
        console.log(this.get("token")); //undefined
    }

Now I probably did something wrong because ... Well it did work when using a Map from immutable.

Question 2 - Is it related to #7 ? Did I fail to build my constructor ? :(
Do you think you could explain it to me so I can help you do some docs ?

Many thanks again for letting us deal with those overriding issues :D

Processing data in constructor

Hi! I have been trying to implement this library in my project and faced an issue with processing data in the constructor.

Essentially, the idea was to create an OrderedMap with id => Map mapping from a raw JS array.

The code for this initialization looks like this

import {OrderedMap} from 'extendable-immutable';

export class Collection extends OrderedMap {
	constructor(raw) {
	    const items = raw.reduce(
	        (reduction, item) => reduction.set(item.id, Immutable.fromJS(item)),
	        Immutable.OrderedMap()
	    );

	    super(items);
	}
}

...

const items = new Collection([{id: 1}, {id: 2}]);

At first everything looked fine, until I've tried to use .filter() on such Collection, and for some reason it doesn't work as expected. Instead I get only one Map, however, if any conversion method is called before .filter(), such as .toList() or .toMap(), filter works as expected.

Could you help me out to figure out the problem?

Best regards,
Thanks.

Doesn't work for classes with a getter

class Shape extends Map {

    //Collision priority represents with Shape should test collision
    //Collision will be testing with the shape with higher priority
    constructor(type, params, collisionPriority=0) {
        super(setProperties({
            type: type,
            collisionPriority: collisionPriority
        }, params), true);
    }

    get type() {
        return this.get("type");
    }

    get collisionPriority() {
        return this.get('collisionPriority');
    }
}

class RegularPolygon extends Shape {

    constructor(numSides, size, vertices=null) {
        super("regularpolygon", {
            numSides: numSides,
            size: size,
            vertices: vertices
        });
    }

    get numSides() {
        return this.get("numSides");
    }

    get size() {
        return this.get("size");
    }
}

Unnecessary code is removed from the classes. setProperties will add properties in params to the object passed to super.

This gives me this error:

extendable.js:44 Uncaught TypeError: Cannot set property size of [object Object] which has only a getter
    at extendable.js:44
    at Array.reduce (<anonymous>)
    at extendable.js:43
    at e.u.__wrapImmutable (createExtendable.js:53)
    at e.a [as constructor] (extendable.js:55)
    at e (Shape.js:11)
    at new e (RegularPolygon.js:9)

I'm looking into fixing it, but I figure it will be easier for someone else. Maybe there's a better way to do what I'm looking for as well.

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.