@okikio/manager
A superset of the Map class, it extends the Map classes capabilities with awesome new features; it weighs ~361 B (minified and gzipped).
You will need a Map and Promise polyfill for older browsers. If you install @okikio/manager
via npm you are most likely going to need rollup or esbuild. You can use polyfill.io, or another source to create a polyfill. The minimum feature requirement for a polyfill are Maps and Promises e.g. https://cdn.polyfill.io/v3/polyfill.min.js?features=Maps,Promise.
You can try out @okikio/manager
using Gitpod:
By default Gitpod will start the dev script for you, but if you need to restart the dev script you can do so by typing into the terminal.
pnpm test-dev --filter "@okikio/manager"
Once Gitpod has booted up, go to ./tests/test.ts and start tweaking and testing to your hearts content.
Table of Contents
Installation
You can install @okikio/manager from npm via npm i @okikio/manager
, pnpm i @okikio/animate
or yarn add @okikio/manager
.
You can use @okikio/manager
on the web via:
Once installed it can be used like this:
import { Manager } from "@okikio/manager";
import { Manager } from "https://unpkg.com/@okikio/manager/lib/api.es.js";
import { Manager } from "https://cdn.jsdelivr.net/npm/@okikio/manager/lib/api.es.js";
import { Manager } from "https://cdn.skypack.dev/@okikio/manager";
<script src="https://unpkg.com/@okikio/manager/lib/api.js"></script>
const { Manager, methodCall } = window.manager;
const { default: Manager, methodCall } = window.manager;
Getting started
The Manager
class makes Maps easier to use, as well as adding 7 methods, getMap, last, methodCall, asyncMethodCall, add, remove, keys and values, (methodCall, and asyncMethodCall are seperate methods from the Manager
class, so treeshaking can get rid of them if they aren't need).
Note: the behavior of the keys and values methods are slightly modified, to return an Array of keys/values instead of an iterator. You can get the keys and values original effects by using .getMap().keys()
or .getMap().values()
.
API Documentation
You can also go through the API documentation, for a more detailed documentation of the API.
API
Existing Map Methods
Manager.prototype.get(key: K): V
Returns a specified element from a Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map object.
Manager.prototype.set(value: V): Manager
Adds or updates an element with a specified key and a value to a Map object. Returns the Manager class (it is chainable).
Note: techincally the values being returned are different from a normal Map. The Map .set()
method returns the Map class while the Manager
.set()
method return the Manager
class, this shouldn't leave much of an effect on use, but should be kept in mind.
Manager.prototype.size: number
Returns the number of elements in a Map object.
Manager.prototype.delete(key: K): boolean
Removes the specified element from a Map object by key. Returns true if an element in the Map object existed and has been removed, or false if the element does not exist.
Manager.prototype.entries(): [[K, V], ...]
Returns a new Iterator object that contains the [key, value] pairs for each element in the Map object in insertion order.
Manager.prototype.has(): boolean
Returns a boolean indicating whether an element with the specified key exists or not, true if an element with the specified key exists in the Map object; otherwise false.
Manager.prototype.forEach(callback: Function,context?: object): Manager
Executes a provided function once per each key/value pair in the Map object, in insertion order. Returns the Manager
class (it is chainable).
Manager#length
Manager.prototype.length
import Manager from "@okikio/manager";
const arr = Array.from([1, 2, 3, 4, 5].entries());
const manager = new Manager(arr);
console.log(manager.length);
Manager#getMap()
Manager.prototype.getMap();
import Manager from "@okikio/manager";
const arr = Array.from([1, 2, 3, 4, 5].entries());
const manager = new Manager(arr);
console.log(manager.getMap());
Manager#add(value)
Manager.prototype.add(value: V);
import Manager from "@okikio/manager";
const arr = Array.from([1, 2, 3, 4, 5].entries());
const manager = new Manager(arr);
manager.add(6).add(7);
console.log(manager.get(5));
Manager#remove(key)
Manager.prototype.remove(key: K);
import Manager from "@okikio/manager";
const arr = Array.from([1, 2, 3, 4, 5].entries());
const manager = new Manager(arr);
manager.remove(2).remove(1);
console.log(manager.get(1));
Manager#keys()
Manager.prototype.keys();
import Manager from "@okikio/manager";
const arr = Array.from([1, 2, 3, 4, 5].entries());
const manager = new Manager(arr);
console.log(manager.keys());
Manager#values()
Manager.prototype.values();
import Manager from "@okikio/manager";
const arr = Array.from([1, 2, 3, 4, 5].entries());
const manager = new Manager(arr);
console.log(manager.values());
Manager#last(distance)
Manager.prototype.last(distance: number = 1);
import Manager from "@okikio/manager";
const arr = Array.from([1, 2, 3, 4, 5].entries());
const manager = new Manager(arr);
console.log(manager.last());
console.log(manager.last(3));
Manager#@@iterator
import Manager from "@okikio/manager";
const arr = Array.from([1, 2, 3, 4, 5].entries());
const manager = new Manager(arr);
for (let [key, value] of manager) {
console.log({ key, value });
}
#methodCall(method, ...)
methodCall(method: string, ...args: any);
import { Manager, methodCall } from "@okikio/manager";
const manager = new Manager();
manager.set("x", { print: console.log });
manager.set("y", { print: console.log });
methodCall(manager, "print", Date.now());
#asyncMethodCall(method, ...)
asyncMethodCall(method: string, ...args: any[]);
import { Manager, asyncMethodCall } from "@okikio/manager";
const manager = new Manager();
let fn = (url = "https://google.com") => {
return async () => {
let data = await fetch(url);
console.log(`(${data.url})`);
};
};
manager.set("x", { print: fn() });
manager.set("y", { print: fn("https://github.com") });
asyncMethodCall(manager, "print");
Contributing
If there is something I missed, a mistake, or a feature you would like added please create an issue or a pull request and I'll try to get to it.
The native
initiative uses Conventional Commits as the style of commit, we also use the Commitizen CLI to make commits easier.
Licence
See the LICENSE file for license rights and limitations (MIT).