Giter Site home page Giter Site logo

nsmap's Introduction

Namespaced Maps

NSMap is a simple map implementation with get() and set() that support namespacing in the key names.

I came across the need for such a data structure where you might need to override the value of a key for a certain scoped condition BUT have the ability get the default, un-scoped values of the key. The namespace is identified by a : before the name.

Basic Example

Let's say we have a following function to print the lunch oder of 3 friends. order is an instance of NSMap and you can access the individual "item" someone ordered by their name as a namespace / scope.

const logOrder = (order) => {
  console.log('John wants', order.get('john:item'));
  console.log('Sara wants', order.get('sara:item'));
  console.log('Raj wants', order.get('raj:item'));
};

If they all ordered the same thing, you can simply do this

const order = new NSMap();
order.set('item', 'burger');
logOrder(order);

Output

John wants burger
Sara wants burger
Raj wants burger

Now let's Sara changes her mind and wants pasta, you can then simply do

const order = new NSMap();
order.set('item', 'burger');
order.set('sara:item', 'pasta'); // setting the value of `item` with a namespcace/scope of `sara`
logOrder(order);

Output

John wants burger
Sara wants pasta
Raj wants burger

Multiple Namespace Example

const m = NSMap();

m.set('name', 'name1');

console.log(m.get('name'));
console.log(m.get('ns1:name'));
console.log(m.get('ns1:ns2:name'));

m.set('ns1:name', 'name2');
m.set('ns1:ns2:name', 'name3');

console.log(m.get('name'));
console.log(m.get('ns1:name'));
console.log(m.get('ns1:ns2:name'));

output

name1
name1
name1
name1
name2
name3

nsmap's People

Contributors

danishm avatar

Stargazers

 avatar

Watchers

 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.