Compare commits

..

14 Commits

Author SHA1 Message Date
749be9ac60 4.0.1 2020-05-25 13:18:53 +00:00
7c9ad26519 fix(core): update 2020-05-25 13:18:53 +00:00
6fdf08c8a9 4.0.0 2020-05-04 00:09:21 +00:00
de04d75e18 BREAKING CHANGE(core): refactored ObjectMap and introduced concat feature 2020-05-04 00:09:20 +00:00
5159d7e4bf 3.0.19 2020-02-16 23:15:51 +00:00
7b78aaea72 fix(FastMap): added .clean() method 2020-02-16 23:15:50 +00:00
54a63d1f41 3.0.18 2020-02-15 21:25:50 +00:00
dd5a009d0f fix(core): update 2020-02-15 21:25:49 +00:00
e7933cd7f4 3.0.17 2020-02-07 16:32:58 +00:00
56a8d9182b fix(core): update 2020-02-07 16:32:58 +00:00
3a971301a4 3.0.16 2020-02-06 15:31:59 +00:00
b880036b64 fix(Objectmap): switch to an object map with unique keys 2020-02-06 15:31:58 +00:00
9d756dbff7 3.0.15 2020-02-06 11:11:27 +00:00
b8111c66ff fix(core): update 2020-02-06 11:11:27 +00:00
13 changed files with 564 additions and 451 deletions

4
.gitignore vendored
View File

@ -15,8 +15,6 @@ node_modules/
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
dist_*/
# custom

View File

@ -24,13 +24,14 @@ mirror:
- docker
- notpriv
snyk:
image: registry.gitlab.com/hosttoday/ht-docker-node:snyk
audit:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --ignore-scripts
- npmci command snyk test
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high
tags:
- lossless
- docker

683
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,10 @@
{
"name": "@pushrocks/lik",
"version": "3.0.14",
"version": "4.0.1",
"private": false,
"description": "light little helpers for node",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"scripts": {
"test": "(tstest test/)",
"build": "(tsbuild)"
@ -20,12 +20,12 @@
},
"homepage": "https://gitlab.com/pushrocks/lik#README",
"devDependencies": {
"@gitzone/tsbuild": "^2.1.17",
"@gitzone/tsbuild": "^2.1.24",
"@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.28",
"@pushrocks/tapbundle": "^3.2.0",
"@types/node": "^13.7.0",
"tslint": "^6.0.0",
"@pushrocks/tapbundle": "^3.2.1",
"@types/node": "^14.0.5",
"tslint": "^6.1.2",
"tslint-config-prettier": "^1.18.0"
},
"dependencies": {
@ -33,6 +33,7 @@
"@pushrocks/smartpromise": "^3.0.6",
"@pushrocks/smartrx": "^2.0.5",
"@pushrocks/smarttime": "^3.0.12",
"@pushrocks/smartunique": "^3.0.3",
"@types/minimatch": "^3.0.3",
"minimatch": "^3.0.4",
"symbol-tree": "^3.2.4"
@ -41,7 +42,8 @@
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_web/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",

47
readme.md Normal file
View File

@ -0,0 +1,47 @@
# @pushrocks/lik
light little helpers for node
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/lik)
* [gitlab.com (source)](https://gitlab.com/pushrocks/lik)
* [github.com (source mirror)](https://github.com/pushrocks/lik)
* [docs (typedoc)](https://pushrocks.gitlab.io/lik/)
## Status for master
[![pipeline status](https://gitlab.com/pushrocks/lik/badges/master/pipeline.svg)](https://gitlab.com/pushrocks/lik/commits/master)
[![coverage report](https://gitlab.com/pushrocks/lik/badges/master/coverage.svg)](https://gitlab.com/pushrocks/lik/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/lik.svg)](https://www.npmjs.com/package/@pushrocks/lik)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/lik/badge.svg)](https://snyk.io/test/npm/@pushrocks/lik)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
## Usage
Use TypeScript for best in class instellisense.
```javascript
// import any tool that you need from lik
import { Stringmap, Objectmap, Observablemap } from 'lik';
```
### class Stringmap
Stringmap allows you to keep track of strings. It allows you to register triggers for certain events
like when a certain string is removed or added to the map
### class Objectmap
Sometimes you need to keep track of objects, but implementing logic for removing, finding or updating is tedious.
Objectmap takes care of keeping track of objects for you.
## Contribution
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

View File

@ -11,7 +11,7 @@ interface ITestObject {
propOne: string;
propTwo: string;
}
let testObjectmap: lik.Objectmap<ITestObject>;
let testObjectmap: lik.ObjectMap<ITestObject>;
let testObject1: ITestObject = {
propOne: 'hello',
propTwo: 'hello2'
@ -22,8 +22,8 @@ let testObject2: ITestObject = {
};
tap.test('new lik.Objectmap() -> should correctly instantiate an Objectmap', async () => {
testObjectmap = new lik.Objectmap<ITestObject>();
expect(testObjectmap).be.instanceof(lik.Objectmap);
testObjectmap = new lik.ObjectMap<ITestObject>();
expect(testObjectmap).be.instanceof(lik.ObjectMap);
});
tap.test('lik.Objectmap.add() -> should correctly add an object to Objectmap', async () => {

View File

@ -1,7 +1,4 @@
import * as plugins from './lik.plugins';
// import modules
export * from './lik.fastmap';
export * from './lik.interestmap';
export * from './lik.limitedarray';
export * from './lik.looptracker';

View File

@ -1,11 +1,82 @@
import * as plugins from './lik.plugins';
/**
* fast map allows for very quick lookups of objects with a unique key
*/
export class FastMap<T> {
private mapObject: { [key: string]: T } = {};
public isUniqueKey() {}
public isUniqueKey(keyArg: string): boolean {
return this.mapObject[keyArg] ? false : true;
}
public addToMap(identifier: string, objectArg: T) {
this.mapObject[identifier] = objectArg;
public addToMap(
keyArg: string,
objectArg: T,
optionsArg?: {
force: boolean;
}
): boolean {
if (this.isUniqueKey(keyArg) || (optionsArg && optionsArg.force)) {
this.mapObject[keyArg] = objectArg;
return true;
} else {
return false;
}
}
public getByKey(keyArg: string) {
return this.mapObject[keyArg];
}
public removeFromMap(keyArg): T {
const removedItem = this.getByKey(keyArg);
delete this.mapObject[keyArg];
return removedItem;
}
public getKeys() {
const keys: string[] = [];
for (const keyArg in this.mapObject) {
if (this.mapObject[keyArg]) {
keys.push(keyArg);
}
}
return keys;
}
public clean() {
this.mapObject = {};
}
/**
* returns a new Objectmap that includes
*/
public concat(fastMapArg: FastMap<T>) {
const concatedFastmap = new FastMap<T>();
for (const key of this.getKeys()) {
concatedFastmap.addToMap(key, this.getByKey(key));
}
for (const key of fastMapArg.getKeys()) {
concatedFastmap.addToMap(key, fastMapArg.getByKey(key), {
force: true
});
}
return concatedFastmap;
}
/**
* tries to merge another Objectmap
* Note: uniqueKeyCollisions will cause overwrite
* @param objectMapArg
*/
public addAllFromOther(fastMapArg: FastMap<T>) {
for (const key of fastMapArg.getKeys()) {
this.addToMap(key, fastMapArg.getByKey(key), {
force: true
});
}
}
}

View File

@ -1,5 +1,5 @@
import * as plugins from './lik.plugins';
import { Objectmap } from './lik.objectmap';
import { ObjectMap } from './lik.objectmap';
import { Observable } from 'rxjs';
@ -11,7 +11,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
/**
* stores interests that are currently fullfilled by the cache
*/
private interestObjectMap = new Objectmap<Interest<DTInterestId, DTInterestFullfillment>>();
private interestObjectMap = new ObjectMap<Interest<DTInterestId, DTInterestFullfillment>>();
/**
* a function to compare interests
@ -106,8 +106,8 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
*/
public findInterest(objectArg: DTInterestId): Interest<DTInterestId, DTInterestFullfillment> {
const comparableString = this.comparisonFunc(objectArg);
const interest = this.interestObjectMap.find(interest => {
return interest.comparisonString === comparableString;
const interest = this.interestObjectMap.find(interestArg => {
return interestArg.comparisonString === comparableString;
});
return interest; // if an interest is found, the interest is returned, otherwise interest is null
}

View File

@ -1,9 +1,9 @@
import * as plugins from './lik.plugins';
import { Objectmap } from './lik.objectmap';
import { ObjectMap } from './lik.objectmap';
export class LoopTracker<T> {
referenceObjectMap = new Objectmap<any>();
referenceObjectMap = new ObjectMap<any>();
constructor() {
// nothing here
}
@ -12,7 +12,12 @@ export class LoopTracker<T> {
* checks and tracks an object
* @param objectArg
*/
checkAndTrack(objectArg: T) {
return this.referenceObjectMap.add(objectArg);
checkAndTrack(objectArg: T): boolean {
if (!this.referenceObjectMap.checkForObject(objectArg)) {
this.referenceObjectMap.add(objectArg);
return true;
} else {
return false;
}
}
}

View File

@ -12,9 +12,8 @@ export interface IObjectmapFindFunction<T> {
/**
* allows keeping track of objects
*/
export class Objectmap<T> {
export class ObjectMap<T> {
private fastMap = new FastMap<T>();
private objectArray: T[] = [];
// events
public eventSubject = new plugins.smartrx.rxjs.Subject<any>();
@ -30,45 +29,51 @@ export class Objectmap<T> {
* adds an object mapped to a string
* the string must be unique
*/
addMappedUnique(uniqueKey: string, objectArg: T) {
this.add(objectArg);
this.fastMap.addToMap(uniqueKey, objectArg);
addMappedUnique(uniqueKeyArg: string, objectArg: T) {
this.fastMap.addToMap(uniqueKeyArg, objectArg);
}
/**
* fastest way to get an object from the map
* @param uniqueKey
*/
public getMappedUnique(uniqueKey: string) {}
public getMappedUnique(uniqueKeyArg: string) {
return this.fastMap.getByKey(uniqueKeyArg);
}
/**
* remove key
* @param functionArg
*/
public removeMappedUnique() {}
public removeMappedUnique(uniqueKey: string) {
const object = this.getMappedUnique(uniqueKey);
}
/**
* add object to Objectmap
* returns false if the object is already in the map
* returns true if the object was added successfully
*/
public add(objectArg: T): boolean {
if (this.checkForObject(objectArg)) {
// the object is already in the objectmap
return false;
} else {
// the object is not yet in the objectmap
this.objectArray.push(objectArg);
this.eventSubject.next('add');
return true;
public add(objectArg: T): string {
// lets search for an existing unique key
for (const keyArg of this.fastMap.getKeys()) {
const object = this.fastMap.getByKey(keyArg);
if (object === objectArg) {
return keyArg;
}
}
// otherwise lets create it
const uniqueKey = plugins.smartunique.shortId();
this.addMappedUnique(uniqueKey, objectArg);
return uniqueKey;
}
/**
* like .add but adds an whole array of objects
*/
public addArray(objectArrayArg: T[]) {
for (let item of objectArrayArg) {
for (const item of objectArrayArg) {
this.add(item);
}
}
@ -76,19 +81,34 @@ export class Objectmap<T> {
/**
* check if object is in Objectmap
*/
public checkForObject(objectArg: T) {
return this.objectArray.indexOf(objectArg) !== -1;
public checkForObject(objectArg: T): boolean {
return !!this.getKeyForObject(objectArg);
}
/**
* get key for object
* @param findFunction
*/
public getKeyForObject(objectArg: T) {
let foundKey: string = null;
for (const keyArg of this.fastMap.getKeys()) {
if (!foundKey && this.fastMap.getByKey(keyArg) === objectArg) {
foundKey = keyArg;
} else {
continue;
}
}
return foundKey;
}
/**
* find object
*/
public find(findFunction: IObjectmapFindFunction<T>) {
const resultArray = this.objectArray.filter(findFunction);
if (resultArray.length > 0) {
return resultArray[0];
} else {
return null;
public find(findFunction: IObjectmapFindFunction<T>): T {
for (const keyArg of this.fastMap.getKeys()) {
if (findFunction(this.fastMap.getByKey(keyArg))) {
return this.getMappedUnique(keyArg);
}
}
}
@ -107,8 +127,8 @@ export class Objectmap<T> {
* run function for each item in Objectmap
*/
public async forEach(functionArg: IObjectmapForEachFunction<T>) {
for (let object of this.objectArray) {
await functionArg(object);
for (const keyArg of this.fastMap.getKeys()) {
await functionArg(this.fastMap.getByKey(keyArg));
}
}
@ -116,9 +136,15 @@ export class Objectmap<T> {
* gets an object in the Observablemap and removes it, so it can't be retrieved again
*/
public getOneAndRemove(): T {
const removedItem = this.objectArray.shift();
this.eventSubject.next('remove');
return removedItem;
const keys = this.fastMap.getKeys();
if (keys.length === 0) {
return null;
} else {
const keyToUse = keys[0];
const removedItem = this.fastMap.removeFromMap(keyToUse);
this.eventSubject.next('remove');
return removedItem;
}
}
/**
@ -126,8 +152,8 @@ export class Objectmap<T> {
*/
public getArray(): T[] {
const returnArray: any[] = [];
for (const objectItem of this.objectArray) {
returnArray.push(objectItem);
for (const keyArg of this.fastMap.getKeys()) {
returnArray.push(this.fastMap.getByKey(keyArg));
}
return returnArray;
}
@ -136,32 +162,47 @@ export class Objectmap<T> {
* check if Objectmap ist empty
*/
public isEmpty(): boolean {
if (this.objectArray.length === 0) {
return true;
} else {
return false;
}
return this.fastMap.getKeys().length === 0;
}
/**
* remove object from Objectmap
*/
public remove(objectArg: T) {
let replacementArray = [];
for (let item of this.objectArray) {
if (item !== objectArg) {
replacementArray.push(item);
}
public remove(objectArg: T): T {
if (this.checkForObject(objectArg)) {
const keyArg = this.getKeyForObject(objectArg);
const removedObject = this.fastMap.removeFromMap(keyArg);
this.eventSubject.next('remove');
return removedObject;
}
this.objectArray = replacementArray;
this.eventSubject.next('remove');
return null;
}
/**
* wipe Objectmap
*/
public wipe() {
this.objectArray = [];
this.eventSubject.next('wiped');
for (const keyArg of this.fastMap.getKeys()) {
this.fastMap.removeFromMap(keyArg);
}
}
/**
* returns a new Objectmap that includes
*/
public concat(objectMapArg: ObjectMap<T>) {
const concattedObjectMap = new ObjectMap<T>();
concattedObjectMap.fastMap.addAllFromOther(this.fastMap);
concattedObjectMap.fastMap.addAllFromOther(objectMapArg.fastMap);
return concattedObjectMap;
}
/**
* tries to merge another Objectmap
* Note: uniqueKeyCollisions will cause overwrite
* @param objectMapArg
*/
public addAllFromOther(objectMapArg: ObjectMap<T>) {
this.fastMap.addAllFromOther(objectMapArg.fastMap);
}
}

View File

@ -13,8 +13,9 @@ import * as smartdelay from '@pushrocks/smartdelay';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrx from '@pushrocks/smartrx';
import * as smarttime from '@pushrocks/smarttime';
import * as smartunique from '@pushrocks/smartunique';
export { smartdelay, smartpromise, smartrx, smarttime };
export { smartdelay, smartpromise, smartrx, smarttime, smartunique };
// ==============
// third party

View File

@ -92,15 +92,20 @@ export class Stringmap {
/**
* register a new trigger
*/
public registerUntilTrue(functionArg: ITriggerFunction, doFunctionArg) {
public registerUntilTrue(functionArg: ITriggerFunction, callbackArg?: () => any) {
const trueDeferred = plugins.smartpromise.defer();
this._triggerUntilTrueFunctionArray.push(() => {
const result = functionArg();
if (result === true) {
doFunctionArg();
if (callbackArg) {
callbackArg();
}
trueDeferred.resolve();
}
return result;
});
this.notifyTrigger();
return trueDeferred.promise;
}
/**