Giter Site home page Giter Site logo

orderedmap's Introduction

OrderedMap

Persistent data structure representing an ordered mapping from strings to values, with some convenient update methods.

This is not an efficient data structure for large maps, just a minimal helper for cleanly creating and managing small maps in a way that makes their key order explicit and easy to think about.

License: MIT

Reference

The exported value from this module is the class OrderedMap, instances of which represent a mapping from strings to arbitrary values.

OrderedMap.from(value: ?Object | OrderedMap) → OrderedMap
Return a map with the given content. If null, create an empty map. If given an ordered map, return that map itself. If given an object, create a map from the object's properties.

Methods

Instances of OrderedMap have the following methods and properties:

get(key: string) → ?any
Retrieve the value stored under key, or return undefined when no such key exists.

update(key: string, value: any, newKey: ?string) → OrderedMap
Create a new map by replacing the value of key with a new value, or adding a binding to the end of the map. If newKey is given, the key of the binding will be replaced with that key.

remove(key: string) → OrderedMap
Return a map with the given key removed, if it existed.

addToStart(key: string, value: any) → OrderedMap
Add a new key to the start of the map.

addToEnd(key: string, value: any) → OrderedMap
Add a new key to the end of the map.

addBefore(place: string, key: value: string, value: any) → OrderedMap
Add a key after the given key. If place is not found, the new key is added to the end.

forEach(f: (key: string, value: any))
Call the given function for each key/value pair in the map, in order.

prepend(map: Object | OrderedMap) → OrderedMap
Create a new map by prepending the keys in this map that don't appear in map before the keys in map.

append(map: Object | OrderedMap) → OrderedMap
Create a new map by appending the keys in this map that don't appear in map after the keys in map.

subtract(map: Object | OrderedMap) → OrderedMap
Create a map containing all the keys in this map that don't appear in map.

toObject() -> Object Return an object that has the same key/value pairs as the map.

size: number
The amount of keys in this map.

orderedmap's People

Contributors

adrianheine avatar andrewvasilchuk avatar lpellegr avatar marijnh avatar mgenware avatar saul-mirone avatar whawker 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

orderedmap's Issues

Any plans for implementing Symbol.iterator support?

WIth modern linters, especially with TypeScript code, (e.g. the unicorn/recommended rules), I've noticed it trying to replace my .forEach loops for a for (const item of orderedMapObject) which breaks my code.
Any reason to not implement support for the Symbol.iterator method so that using for() loops will work smoothly with this library?
If this is accepted, I don't PR-ing a fix for this.

Use named export for ES module compatibility

Currently this package exports a class as the module, which isn't supported in ES modules. A simple solution is to just use a named export, i.e.

exports.OrderedMap = OrderedMap;

Commonjs consumers would then import via:

const { OrderedMap } = require('orderedmap');

The motivation for this is to smooth the path for packages wanting to adopt ES modules that depend on this package.

[feature request] Add support for toObject in the from function

The from function should use the value of a 'toObject' function if it exists on the given object. This will add compatibility with other mapped classes, and more importantly, make orderedmap compatible with other versions of itself (that also have the toObject function) so that it does not need to be a shared dependency (for consumers using the newer version).

example implementation (checks that a 0 argument toObject function exists and then calls it)
if (value && typeof value.toObject === 'function' && value.toObject.length === 0) value = value.toObject()

Error with TypeScript 3.5, "An accessor cannot be declared in an ambient context."

The types introduced in #6 include a getter that is not supported in TypeScript versions 3.6 and earlier. This particular change in the compiler output was widely criticized at the time.

While these TypeScript versions are pretty old (~2 years) by now, they're still used by some projects. Our particular issue is with the Kendo UI Editor for Angular when running in Angular 8.x projects, but it may not be limited to it.

My suggestion is to replace it with a readonly field to remain compatible with older versions of TypeScript.

The only downside to that is that inheriting OrderedMap will require using a field for overriding size. I'm not sure if inheriting the class is a common scenario for the library. Edit: I've just noticed that the constructor is marked as private and extending the class is not supported.

diff --git a/types/index.d.ts b/types/index.d.ts
index 551e742..fdc372c 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -21,7 +21,7 @@ declare class OrderedMap<T = any> {
 
   subtract(map: MapLike<T>): OrderedMap<T>
 
-  get size(): number
+  readonly size: number
 
   static from<T>(map: MapLike<T>): OrderedMap<T>
 }

This module probably needs to be updated to account for the new `linebreakReplacement` property

When I upgrade prosemirror-model to latest version, I get type errors. It complains of some incompatibility between modules

Type 'import("/project/node_modules/orderedmap/dist/index").default<import("/project/packages/prosemirror/node_modules/prosemirror-model/dist/index").NodeSpec>' is not assignable to type 'import("/project/node_modules/orderedmap/dist/index").default<import("/project/node_modules/prosemirror-model/dist/index").NodeSpec>'
...
Property 'linebreakReplacement' is missing in type 'import("/project/node_modules/prosemirror-model/dist/index").Schema<any, any>' but required in type 'import("/project/packages/prosemirror/node_modules/prosemirror-model/dist/index").Schema<any, any>'

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.