Map with order functionality, providing fast look ups and ordering in the same time with O(n)
complexity. Newer TS implementation. DeepClones with typesafety. Customizable validator/marshaller and deepClone available.
npm install ordered-fast-lookup-map
Runs with default validator
and deepClone
set to false
;
var orderedMap = require("ordered-fast-lookup-map")
var userMap = new orderedMap();
We are now providing the types in the lib.
constructor(keys?: NumberOrString[] | number[], values?: T[], options: IOFMLoptions<T> = { deepClone: false })
- when supplied withkeys
andvalues
initiates structure and performpush
on pair of keys. Exampple:var hashMap = OrderedTyoedFastLookupMap<string>([1,2,3],["a","b","c"]);
. Additionallyoptions:IOFMLoptions
object can be passed, to set custom validator, to set deep clone and provide custom deep link handler.
set(key: NumberOrString, value: T)
sets the value on the end of structurepush(key: NumberOrString, value: T)
alias for setunshift(key: NumberOrString, value: T)
sets the value on the beginning of structurearbitrarySetAfter(afterKey: NumberOrString, key: NumberOrString, value: T)
sets the value after arbitrary supplied keys. If the arbitrary key is not found it throwsError
.arbitrarySetBefore(beforeKey: NumberOrString, key: NumberOrString, value: T)
sets the value before arbitrary supplied keys. If the arbitrary key is not found it throwsError
.
Note if inserting undefined
any method will throw exception. Please use null instead. Or change the validator
methods. Validator now can either throw
or return true/false
. This applies to all places where method is marked as throwable
remove(key: NumberOrString)
remove arbitrary value on the key. If the arbitrary key is not found it throwsError
.pop(): T
returns last element of structure and removes it. If list is empty returnsundefined
shift(): T
returns first element of structure and removes it. If list is empty returnsundefined
get(key: NumberOrString): T
returns value on the key without deleting it (return reference). If element is not found returnsundefined
has(key: NumberOrString): boolean
checks if key exists (true/false
)
forEach(callback: IteratorCallback<T>): void
for each element inasc
order executes user suppliedfunction(index,value,validatorFunction)
. To break in user function returntrue
. We are now exposing the validator method out.forEachReverse(callback: IteratorCallback<T>): void
for each element indesc
order executes user suppliedfunction(index,value,validatorFunction)
. To break in user function returntrue
. We are now exposing the validator method out.
-
private validator(value: T): void | boolean
by default, throws error is value isundefined
. Can be replaced withIOFMLoptions
-
private deepCloneMethod(val: T): T
when deepClone is set totrue
(by default isfalse
) this gets executed to clone object with keeping theconstructor.name
to preserve es6 object type/instance type. Can be replaced withIOFMLoptions
type IteratorCallback<T> = (key: string, value: T, validator: IValidatorFunction<T>) => boolean;
type IValidatorFunction<T> = (value: T) => void | boolean;
type IDeepCloneFunction<T> = (value: T) => T;
type NumberOrString = string | number;
type IOFMLoptions<T> = {
validator?: IValidatorFunction<T>
deepClone?: boolean // default to false
deepCloneMethod?: IDeepCloneFunction<T>
}
New TS implementation should work as previous v1.1.2 when accessed through require
. It assumes OrderedTyoedFastLookupMap<any>
with default validator that throws error only when you try to set undefined
. Also the key
is always forced to be a string in the map (this might be breaking for some).
import { OrderedTypedFastLookupMap, OrderedFastLookupMap } from "ordered-fast-lookup-map/lib";
// this is equivalent to const oflm = new OrderedFastLookupMap([`key`], [[{ "foo": "bar" }]]);
const oflm = new OrderedTypedFastLookupMap<any[]>([`key`], [[{ "foo": "bar" }]]);
console.log(oflm._array) // ["key"];
console.log(oflm.map); // { "key": [{ "foo": "bar" }] }
when imported in TS should allow for strict control of stored types, and the validator
method. validator now can throw(default)
or return true/false
.
import { OrderedTyoedFastLookupMap, IOFMLoptions } from "../lib";
class testClass {
constructor(public x: string) { }
}
class testClassWrong {
constructor(public x: string) { }
}
const foo = new testClass('bar');
const noo = new testClassWrong('bar');
// adds foo t map
const oflm = new OrderedTyoedFastLookupMap<testClass>([1], foo);
...
// ts error
const fail = new OrderedTyoedFastLookupMap<testClass>([1], noo);
...
// silently fails to add and just intiates the map
const fail = new OrderedTyoedFastLookupMap<testClass>([1], <any>noo);
...
// changes default validator
const opts: IOFMLoptions<testClass> = {
validator: (val) => {
if (!(<any>val instanceof testClass)) {
return false;
}
return true;
}
};
// silently fails to add and just intiates the map
const oflmValidatr = new OrderedTyoedFastLookupMap<testClass>([1], <testClass>noo, opts);
...
// changes default validator
const opts: IOFMLoptions<testClass> = {
validator: (val) => {
return true;
}
};
// This will allow to create testClassWrong in the map and ignore marshalling
// checks, ts still will assumed the templated type
const oflmValidatr = new OrderedTyoedFastLookupMap<testClass>([1], <testClass>noo, opts);
...
const opts: IOFMLoptions<testClass> = {
// deepCloneMethod <====== hookup custom method
deepClone: true,
validator: (val) => {
if (!(<any>val instanceof testClass)) {
return false;
}
return true;
}
};