Compare commits
36 Commits
Author | SHA1 | Date | |
---|---|---|---|
e551a68237 | |||
f402e55ff3 | |||
15b8fe406a | |||
c7557163cd | |||
f84822dd5d | |||
21d6e19a22 | |||
8947738dc1 | |||
14bc5dcd25 | |||
c2f366de7f | |||
ae47276569 | |||
42b59c109d | |||
8f18faaf1c | |||
ccd5b80d67 | |||
298904e17e | |||
fa91e67aef | |||
5e99066bee | |||
a838f7eb80 | |||
979e93be27 | |||
60587f052c | |||
da5bd43a42 | |||
24559d1582 | |||
d92657b130 | |||
f8f34bf8a3 | |||
861d2c04b3 | |||
5845d74160 | |||
9dd952e6b7 | |||
742a711359 | |||
8878a0ef8b | |||
6a52e81cdd | |||
757e709f3f | |||
e0d9c3a3e0 | |||
150953adaf | |||
e0eba5d206 | |||
d9eb836f98 | |||
6d29798660 | |||
f80a84086c |
@ -19,23 +19,36 @@ mirror:
|
|||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
audit:
|
auditProductionDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci command npm install --production --ignore-scripts
|
||||||
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
|
auditDevDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
- npmci command npm audit --audit-level=high
|
- npmci command npm audit --audit-level=high --only=dev
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
@ -50,9 +63,7 @@ testStable:
|
|||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- priv
|
|
||||||
|
|
||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
@ -63,9 +74,7 @@ testBuild:
|
|||||||
- npmci command npm run build
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
@ -85,6 +94,8 @@ release:
|
|||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g tslint typescript
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -15,7 +15,7 @@
|
|||||||
"properties": {
|
"properties": {
|
||||||
"projectType": {
|
"projectType": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": ["website", "element", "service", "npm"]
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26075
package-lock.json
generated
26075
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
32
package.json
32
package.json
@ -1,13 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/lik",
|
"name": "@pushrocks/lik",
|
||||||
"version": "4.0.5",
|
"version": "5.0.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "light little helpers for node",
|
"description": "light little helpers for node",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/)",
|
||||||
"build": "(tsbuild && tsbundle npm)"
|
"build": "(tsbuild --web && tsbundle npm)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -20,23 +20,22 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/lik#README",
|
"homepage": "https://gitlab.com/pushrocks/lik#README",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.24",
|
"@gitzone/tsbuild": "^2.1.27",
|
||||||
"@gitzone/tsbundle": "^1.0.69",
|
"@gitzone/tsbundle": "^1.0.87",
|
||||||
"@gitzone/tsrun": "^1.2.8",
|
"@gitzone/tsrun": "^1.2.17",
|
||||||
"@gitzone/tstest": "^1.0.28",
|
"@gitzone/tstest": "^1.0.57",
|
||||||
"@pushrocks/tapbundle": "^3.2.1",
|
"@pushrocks/tapbundle": "^3.2.14",
|
||||||
"@types/node": "^14.0.5",
|
"@types/node": "^16.9.1",
|
||||||
"tslint": "^6.1.2",
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^2.0.6",
|
"@pushrocks/smartdelay": "^2.0.13",
|
||||||
"@pushrocks/smartmatch": "^1.0.7",
|
"@pushrocks/smartmatch": "^1.0.7",
|
||||||
"@pushrocks/smartpromise": "^3.0.6",
|
"@pushrocks/smartpromise": "^3.1.6",
|
||||||
"@pushrocks/smartrx": "^2.0.5",
|
"@pushrocks/smartrx": "^2.0.19",
|
||||||
"@pushrocks/smarttime": "^3.0.15",
|
"@pushrocks/smarttime": "^3.0.38",
|
||||||
"@pushrocks/smartunique": "^3.0.3",
|
"@types/minimatch": "^3.0.5",
|
||||||
"@types/minimatch": "^3.0.3",
|
|
||||||
"symbol-tree": "^3.2.4"
|
"symbol-tree": "^3.2.4"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
@ -50,5 +49,8 @@
|
|||||||
"cli.js",
|
"cli.js",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
21
readme.md
21
readme.md
@ -8,13 +8,20 @@ light little helpers for node
|
|||||||
* [docs (typedoc)](https://pushrocks.gitlab.io/lik/)
|
* [docs (typedoc)](https://pushrocks.gitlab.io/lik/)
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
[](https://gitlab.com/pushrocks/lik/commits/master)
|
|
||||||
[](https://gitlab.com/pushrocks/lik/commits/master)
|
Status Category | Status Badge
|
||||||
[](https://www.npmjs.com/package/@pushrocks/lik)
|
-- | --
|
||||||
[](https://snyk.io/test/npm/@pushrocks/lik)
|
GitLab Pipelines | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
npm | [](https://lossless.cloud)
|
||||||
[](https://prettier.io/)
|
Snyk | [](https://lossless.cloud)
|
||||||
|
TypeScript Support | [](https://lossless.cloud)
|
||||||
|
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
Code Style | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||||
|
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||||
|
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
28
test/test.fastmap.both.ts
Normal file
28
test/test.fastmap.both.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
|
import * as lik from '../ts';
|
||||||
|
|
||||||
|
tap.test('should create a valid fastmap', async () => {
|
||||||
|
const fastmap = new lik.FastMap();
|
||||||
|
expect(fastmap).to.be.instanceOf(lik.FastMap);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should find an entry', async () => {
|
||||||
|
const fastmap = new lik.FastMap<{
|
||||||
|
value1: string;
|
||||||
|
value2: string;
|
||||||
|
}>();
|
||||||
|
fastmap.addToMap('heythere', {
|
||||||
|
value1: 'heyho',
|
||||||
|
value2: 'heyho2'
|
||||||
|
})
|
||||||
|
fastmap.addToMap('heythere2', {
|
||||||
|
value1: 'heyho3',
|
||||||
|
value2: 'heyho4'
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await fastmap.find(async (itemArg)=> {return itemArg.value2 === 'heyho4'});
|
||||||
|
expect(result.value1).to.equal('heyho3');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
@ -4,7 +4,7 @@ import * as lik from '../ts/index';
|
|||||||
let testInterestmap: lik.InterestMap<number, number>;
|
let testInterestmap: lik.InterestMap<number, number>;
|
||||||
|
|
||||||
tap.test('should create an interestmap', async () => {
|
tap.test('should create an interestmap', async () => {
|
||||||
testInterestmap = new lik.InterestMap(numberArg => {
|
testInterestmap = new lik.InterestMap((numberArg) => {
|
||||||
return numberArg.toString();
|
return numberArg.toString();
|
||||||
});
|
});
|
||||||
});
|
});
|
@ -14,11 +14,11 @@ interface ITestObject {
|
|||||||
let testObjectmap: lik.ObjectMap<ITestObject>;
|
let testObjectmap: lik.ObjectMap<ITestObject>;
|
||||||
let testObject1: ITestObject = {
|
let testObject1: ITestObject = {
|
||||||
propOne: 'hello',
|
propOne: 'hello',
|
||||||
propTwo: 'hello2'
|
propTwo: 'hello2',
|
||||||
};
|
};
|
||||||
let testObject2: ITestObject = {
|
let testObject2: ITestObject = {
|
||||||
propOne: 'hello',
|
propOne: 'hello',
|
||||||
propTwo: 'hello2'
|
propTwo: 'hello2',
|
||||||
};
|
};
|
||||||
|
|
||||||
tap.test('new lik.Objectmap() -> should correctly instantiate an Objectmap', async () => {
|
tap.test('new lik.Objectmap() -> should correctly instantiate an Objectmap', async () => {
|
||||||
@ -44,7 +44,7 @@ tap.test('lik.Objectmap.remove() -> should correctly remove an object to Objectm
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('Objectmap.forEach -> should correctly run a function forEach map object', async () => {
|
tap.test('Objectmap.forEach -> should correctly run a function forEach map object', async () => {
|
||||||
testObjectmap.forEach(itemArg => {
|
testObjectmap.forEach((itemArg) => {
|
||||||
expect(itemArg).to.have.property('propOne');
|
expect(itemArg).to.have.property('propOne');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -52,7 +52,7 @@ tap.test('Objectmap.forEach -> should correctly run a function forEach map objec
|
|||||||
tap.test('lik.Objectmap.find() -> should correctly find an object', async () => {
|
tap.test('lik.Objectmap.find() -> should correctly find an object', async () => {
|
||||||
let myObject = { propOne: 'helloThere', propTwo: 'helloAnyway' };
|
let myObject = { propOne: 'helloThere', propTwo: 'helloAnyway' };
|
||||||
testObjectmap.add(myObject);
|
testObjectmap.add(myObject);
|
||||||
let referenceObject = testObjectmap.find(itemArg => {
|
let referenceObject = await testObjectmap.find(async (itemArg) => {
|
||||||
return itemArg.propOne === 'helloThere';
|
return itemArg.propOne === 'helloThere';
|
||||||
});
|
});
|
||||||
// tslint:disable-next-line:no-unused-expression
|
// tslint:disable-next-line:no-unused-expression
|
@ -8,12 +8,12 @@ import * as lik from '../ts/index';
|
|||||||
|
|
||||||
let testTimedAggregator: lik.TimedAggregtor<string>;
|
let testTimedAggregator: lik.TimedAggregtor<string>;
|
||||||
|
|
||||||
tap.test('should create a timed aggregaotor', async tools => {
|
tap.test('should create a timed aggregaotor', async (tools) => {
|
||||||
testTimedAggregator = new lik.TimedAggregtor<string>({
|
testTimedAggregator = new lik.TimedAggregtor<string>({
|
||||||
aggregationIntervalInMillis: 1000,
|
aggregationIntervalInMillis: 1000,
|
||||||
functionForAggregation: aggregation => {
|
functionForAggregation: (aggregation) => {
|
||||||
console.log(aggregation);
|
console.log(aggregation);
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
testTimedAggregator.add('This');
|
testTimedAggregator.add('This');
|
||||||
testTimedAggregator.add('is a whole sentence.');
|
testTimedAggregator.add('is a whole sentence.');
|
@ -1 +0,0 @@
|
|||||||
import './test.objectmap';
|
|
@ -1,5 +1,6 @@
|
|||||||
export * from './lik.fastmap';
|
export * from './lik.fastmap';
|
||||||
export * from './lik.interestmap';
|
export * from './lik.interestmap';
|
||||||
|
export * from './lik.interestmap.interest';
|
||||||
export * from './lik.limitedarray';
|
export * from './lik.limitedarray';
|
||||||
export * from './lik.looptracker';
|
export * from './lik.looptracker';
|
||||||
export * from './lik.objectmap';
|
export * from './lik.objectmap';
|
||||||
|
@ -29,7 +29,7 @@ export class FastMap<T> {
|
|||||||
return this.mapObject[keyArg];
|
return this.mapObject[keyArg];
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeFromMap(keyArg): T {
|
public removeFromMap(keyArg: string): T {
|
||||||
const removedItem = this.getByKey(keyArg);
|
const removedItem = this.getByKey(keyArg);
|
||||||
delete this.mapObject[keyArg];
|
delete this.mapObject[keyArg];
|
||||||
return removedItem;
|
return removedItem;
|
||||||
@ -50,7 +50,7 @@ export class FastMap<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns a new Objectmap that includes
|
* returns a new Fastmap that includes all values from this and all from the fastmap in the argument
|
||||||
*/
|
*/
|
||||||
public concat(fastMapArg: FastMap<T>) {
|
public concat(fastMapArg: FastMap<T>) {
|
||||||
const concatedFastmap = new FastMap<T>();
|
const concatedFastmap = new FastMap<T>();
|
||||||
@ -60,7 +60,7 @@ export class FastMap<T> {
|
|||||||
|
|
||||||
for (const key of fastMapArg.getKeys()) {
|
for (const key of fastMapArg.getKeys()) {
|
||||||
concatedFastmap.addToMap(key, fastMapArg.getByKey(key), {
|
concatedFastmap.addToMap(key, fastMapArg.getByKey(key), {
|
||||||
force: true
|
force: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,15 +68,25 @@ export class FastMap<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* tries to merge another Objectmap
|
* tries to merge another Fastmap
|
||||||
* Note: uniqueKeyCollisions will cause overwrite
|
* Note: uniqueKeyCollisions will cause overwrite
|
||||||
* @param objectMapArg
|
* @param fastMapArg
|
||||||
*/
|
*/
|
||||||
public addAllFromOther(fastMapArg: FastMap<T>) {
|
public addAllFromOther(fastMapArg: FastMap<T>) {
|
||||||
for (const key of fastMapArg.getKeys()) {
|
for (const key of fastMapArg.getKeys()) {
|
||||||
this.addToMap(key, fastMapArg.getByKey(key), {
|
this.addToMap(key, fastMapArg.getByKey(key), {
|
||||||
force: true
|
force: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async find(findFunctionArg: (mapItemArg: T) => Promise<boolean>) {
|
||||||
|
for (const key of this.getKeys()) {
|
||||||
|
const item = this.getByKey(key);
|
||||||
|
const findFunctionResult = await findFunctionArg(item);
|
||||||
|
if (findFunctionResult) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,14 @@ import * as plugins from './lik.plugins';
|
|||||||
|
|
||||||
import { InterestMap, IInterestComparisonFunc } from './lik.interestmap';
|
import { InterestMap, IInterestComparisonFunc } from './lik.interestmap';
|
||||||
|
|
||||||
|
export interface IInterestOptions<DTInterestFullfillment> {
|
||||||
|
markLostAfterDefault: number;
|
||||||
|
defaultFullfillment?: DTInterestFullfillment;
|
||||||
|
}
|
||||||
|
|
||||||
export class Interest<DTInterestId, DTInterestFullfillment> {
|
export class Interest<DTInterestId, DTInterestFullfillment> {
|
||||||
|
public options: IInterestOptions<DTInterestFullfillment>;
|
||||||
|
|
||||||
private interestMapRef: InterestMap<DTInterestId, DTInterestFullfillment>;
|
private interestMapRef: InterestMap<DTInterestId, DTInterestFullfillment>;
|
||||||
public originalInterest: DTInterestId;
|
public originalInterest: DTInterestId;
|
||||||
public comparisonFunc: IInterestComparisonFunc<DTInterestId>;
|
public comparisonFunc: IInterestComparisonFunc<DTInterestId>;
|
||||||
@ -41,14 +48,20 @@ export class Interest<DTInterestId, DTInterestFullfillment> {
|
|||||||
constructor(
|
constructor(
|
||||||
interestMapArg: InterestMap<DTInterestId, DTInterestFullfillment>,
|
interestMapArg: InterestMap<DTInterestId, DTInterestFullfillment>,
|
||||||
interestArg: DTInterestId,
|
interestArg: DTInterestId,
|
||||||
comparisonFuncArg: IInterestComparisonFunc<DTInterestId>
|
comparisonFuncArg: IInterestComparisonFunc<DTInterestId>,
|
||||||
|
optionsArg?: IInterestOptions<DTInterestFullfillment>
|
||||||
) {
|
) {
|
||||||
|
this.interestMapRef = interestMapArg;
|
||||||
this.originalInterest = interestArg;
|
this.originalInterest = interestArg;
|
||||||
this.comparisonFunc = comparisonFuncArg;
|
this.comparisonFunc = comparisonFuncArg;
|
||||||
this.interestMapRef = interestMapArg;
|
this.options = optionsArg;
|
||||||
|
|
||||||
this.destructionTimer.completed.then(() => {
|
this.destructionTimer.completed.then(() => {
|
||||||
this.destroy();
|
this.destroy();
|
||||||
});
|
});
|
||||||
|
if (this.options?.markLostAfterDefault) {
|
||||||
|
plugins.smartdelay.delayFor(this.options.markLostAfterDefault).then(this.markLost);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===============================
|
// ===============================
|
||||||
@ -60,6 +73,9 @@ export class Interest<DTInterestId, DTInterestFullfillment> {
|
|||||||
*/
|
*/
|
||||||
public destroy() {
|
public destroy() {
|
||||||
this.interestMapRef.removeInterest(this);
|
this.interestMapRef.removeInterest(this);
|
||||||
|
if (!this.isFullfilled && this.options.defaultFullfillment) {
|
||||||
|
this.fullfillInterest(this.options.defaultFullfillment);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,7 +7,13 @@ import { Interest } from './lik.interestmap.interest';
|
|||||||
|
|
||||||
export type IInterestComparisonFunc<T> = (objectArg: T) => string;
|
export type IInterestComparisonFunc<T> = (objectArg: T) => string;
|
||||||
|
|
||||||
|
export interface IInterestMapOptions {
|
||||||
|
markLostAfterDefault?: number;
|
||||||
|
}
|
||||||
|
|
||||||
export class InterestMap<DTInterestId, DTInterestFullfillment> {
|
export class InterestMap<DTInterestId, DTInterestFullfillment> {
|
||||||
|
public options: IInterestMapOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* stores interests that are currently fullfilled by the cache
|
* stores interests that are currently fullfilled by the cache
|
||||||
*/
|
*/
|
||||||
@ -18,8 +24,12 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
|
|||||||
*/
|
*/
|
||||||
private comparisonFunc: IInterestComparisonFunc<DTInterestId>;
|
private comparisonFunc: IInterestComparisonFunc<DTInterestId>;
|
||||||
|
|
||||||
constructor(comparisonFuncArg: IInterestComparisonFunc<DTInterestId>) {
|
constructor(
|
||||||
|
comparisonFuncArg: IInterestComparisonFunc<DTInterestId>,
|
||||||
|
optionsArg: IInterestMapOptions = {}
|
||||||
|
) {
|
||||||
this.comparisonFunc = comparisonFuncArg;
|
this.comparisonFunc = comparisonFuncArg;
|
||||||
|
this.options = optionsArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,17 +37,22 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
|
|||||||
* @param objectArg
|
* @param objectArg
|
||||||
*/
|
*/
|
||||||
public async addInterest(
|
public async addInterest(
|
||||||
objectArg: DTInterestId
|
objectArg: DTInterestId,
|
||||||
|
defaultFullfillmentArg?: DTInterestFullfillment
|
||||||
): Promise<Interest<DTInterestId, DTInterestFullfillment>> {
|
): Promise<Interest<DTInterestId, DTInterestFullfillment>> {
|
||||||
const comparisonString = this.comparisonFunc(objectArg);
|
const comparisonString = this.comparisonFunc(objectArg);
|
||||||
let returnInterest: Interest<DTInterestId, DTInterestFullfillment>;
|
let returnInterest: Interest<DTInterestId, DTInterestFullfillment>;
|
||||||
const newInterest = new Interest<DTInterestId, DTInterestFullfillment>(
|
const newInterest = new Interest<DTInterestId, DTInterestFullfillment>(
|
||||||
this,
|
this,
|
||||||
objectArg,
|
objectArg,
|
||||||
this.comparisonFunc
|
this.comparisonFunc,
|
||||||
|
{
|
||||||
|
markLostAfterDefault: this.options.markLostAfterDefault,
|
||||||
|
defaultFullfillment: defaultFullfillmentArg,
|
||||||
|
}
|
||||||
);
|
);
|
||||||
let interestExists = false;
|
let interestExists = false;
|
||||||
await this.interestObjectMap.forEach(interestArg => {
|
await this.interestObjectMap.forEach((interestArg) => {
|
||||||
if (!interestExists && interestArg.comparisonString === newInterest.comparisonString) {
|
if (!interestExists && interestArg.comparisonString === newInterest.comparisonString) {
|
||||||
console.log('info', `interest already exists for ${newInterest.comparisonString}`);
|
console.log('info', `interest already exists for ${newInterest.comparisonString}`);
|
||||||
interestExists = true;
|
interestExists = true;
|
||||||
@ -61,7 +76,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
|
|||||||
* @param objectArg removes an interest from the InterestMap
|
* @param objectArg removes an interest from the InterestMap
|
||||||
*/
|
*/
|
||||||
public removeInterest(interestArg: Interest<DTInterestId, DTInterestFullfillment>) {
|
public removeInterest(interestArg: Interest<DTInterestId, DTInterestFullfillment>) {
|
||||||
const interestToRemove = this.interestObjectMap.findOneAndRemove(interestArg2 => {
|
const interestToRemove = this.interestObjectMap.findOneAndRemoveSync((interestArg2) => {
|
||||||
return interestArg.comparisonString === interestArg2.comparisonString;
|
return interestArg.comparisonString === interestArg2.comparisonString;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -79,7 +94,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
|
|||||||
* @param comparisonStringArg
|
* @param comparisonStringArg
|
||||||
*/
|
*/
|
||||||
public checkInterestByString(comparisonStringArg: string): boolean {
|
public checkInterestByString(comparisonStringArg: string): boolean {
|
||||||
const foundInterest = this.interestObjectMap.find(interest => {
|
const foundInterest = this.interestObjectMap.findSync((interest) => {
|
||||||
return interest.comparisonString === comparisonStringArg;
|
return interest.comparisonString === comparisonStringArg;
|
||||||
});
|
});
|
||||||
if (foundInterest) {
|
if (foundInterest) {
|
||||||
@ -106,7 +121,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
|
|||||||
*/
|
*/
|
||||||
public findInterest(objectArg: DTInterestId): Interest<DTInterestId, DTInterestFullfillment> {
|
public findInterest(objectArg: DTInterestId): Interest<DTInterestId, DTInterestFullfillment> {
|
||||||
const comparableString = this.comparisonFunc(objectArg);
|
const comparableString = this.comparisonFunc(objectArg);
|
||||||
const interest = this.interestObjectMap.find(interestArg => {
|
const interest = this.interestObjectMap.findSync((interestArg) => {
|
||||||
return interestArg.comparisonString === comparableString;
|
return interestArg.comparisonString === comparableString;
|
||||||
});
|
});
|
||||||
return interest; // if an interest is found, the interest is returned, otherwise interest is null
|
return interest; // if an interest is found, the interest is returned, otherwise interest is null
|
||||||
|
@ -1,14 +1,31 @@
|
|||||||
import * as plugins from './lik.plugins';
|
import * as plugins from './lik.plugins';
|
||||||
import { FastMap } from './lik.fastmap';
|
import { FastMap } from './lik.fastmap';
|
||||||
|
|
||||||
|
export const uni = (prefix: string = 'uni') => {
|
||||||
|
return `${prefix}xxxxxxxxxxx`.replace(/[xy]/g, (c) => {
|
||||||
|
const r = (Math.random() * 16) | 0;
|
||||||
|
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
||||||
|
return v.toString(16);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export interface IObjectmapForEachFunction<T> {
|
export interface IObjectmapForEachFunction<T> {
|
||||||
(itemArg: T): void;
|
(itemArg: T): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IObjectmapFindFunction<T> {
|
export interface IObjectmapFindFunctionSync<T> {
|
||||||
(itemArg: T): boolean;
|
(itemArg: T): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IObjectmapFindFunction<T> {
|
||||||
|
(itemArg: T): Promise<boolean>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IObjectMapEventData<T> {
|
||||||
|
operation: 'add' | 'remove';
|
||||||
|
payload: T;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* allows keeping track of objects
|
* allows keeping track of objects
|
||||||
*/
|
*/
|
||||||
@ -16,7 +33,7 @@ export class ObjectMap<T> {
|
|||||||
private fastMap = new FastMap<T>();
|
private fastMap = new FastMap<T>();
|
||||||
|
|
||||||
// events
|
// events
|
||||||
public eventSubject = new plugins.smartrx.rxjs.Subject<any>();
|
public eventSubject = new plugins.smartrx.rxjs.Subject<IObjectMapEventData<T>>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns a new instance
|
* returns a new instance
|
||||||
@ -64,8 +81,12 @@ export class ObjectMap<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// otherwise lets create it
|
// otherwise lets create it
|
||||||
const uniqueKey = plugins.smartunique.shortId();
|
const uniqueKey = uni('key');
|
||||||
this.addMappedUnique(uniqueKey, objectArg);
|
this.addMappedUnique(uniqueKey, objectArg);
|
||||||
|
this.eventSubject.next({
|
||||||
|
operation: 'add',
|
||||||
|
payload: objectArg
|
||||||
|
});
|
||||||
return uniqueKey;
|
return uniqueKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +125,11 @@ export class ObjectMap<T> {
|
|||||||
/**
|
/**
|
||||||
* find object
|
* find object
|
||||||
*/
|
*/
|
||||||
public find(findFunction: IObjectmapFindFunction<T>): T {
|
public async find(findFunction: IObjectmapFindFunction<T>): Promise<T> {
|
||||||
|
return this.fastMap.find(findFunction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public findSync(findFunction: IObjectmapFindFunctionSync<T>): T {
|
||||||
for (const keyArg of this.fastMap.getKeys()) {
|
for (const keyArg of this.fastMap.getKeys()) {
|
||||||
if (findFunction(this.fastMap.getByKey(keyArg))) {
|
if (findFunction(this.fastMap.getByKey(keyArg))) {
|
||||||
return this.getMappedUnique(keyArg);
|
return this.getMappedUnique(keyArg);
|
||||||
@ -115,8 +140,15 @@ export class ObjectMap<T> {
|
|||||||
/**
|
/**
|
||||||
* finds a specific element and then removes it
|
* finds a specific element and then removes it
|
||||||
*/
|
*/
|
||||||
public findOneAndRemove(findFunction: IObjectmapFindFunction<T>): T {
|
public async findOneAndRemove(findFunction: IObjectmapFindFunction<T>): Promise<T> {
|
||||||
const foundElement = this.find(findFunction);
|
const foundElement = await this.find(findFunction);
|
||||||
|
if (foundElement) {
|
||||||
|
this.remove(foundElement);
|
||||||
|
}
|
||||||
|
return foundElement;
|
||||||
|
}
|
||||||
|
public findOneAndRemoveSync(findFunction: IObjectmapFindFunctionSync<T>): T {
|
||||||
|
const foundElement = this.findSync(findFunction);
|
||||||
if (foundElement) {
|
if (foundElement) {
|
||||||
this.remove(foundElement);
|
this.remove(foundElement);
|
||||||
}
|
}
|
||||||
@ -142,7 +174,10 @@ export class ObjectMap<T> {
|
|||||||
} else {
|
} else {
|
||||||
const keyToUse = keys[0];
|
const keyToUse = keys[0];
|
||||||
const removedItem = this.fastMap.removeFromMap(keyToUse);
|
const removedItem = this.fastMap.removeFromMap(keyToUse);
|
||||||
this.eventSubject.next('remove');
|
this.eventSubject.next({
|
||||||
|
operation: 'remove',
|
||||||
|
payload: removedItem
|
||||||
|
});
|
||||||
return removedItem;
|
return removedItem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -172,7 +207,10 @@ export class ObjectMap<T> {
|
|||||||
if (this.checkForObject(objectArg)) {
|
if (this.checkForObject(objectArg)) {
|
||||||
const keyArg = this.getKeyForObject(objectArg);
|
const keyArg = this.getKeyForObject(objectArg);
|
||||||
const removedObject = this.fastMap.removeFromMap(keyArg);
|
const removedObject = this.fastMap.removeFromMap(keyArg);
|
||||||
this.eventSubject.next('remove');
|
this.eventSubject.next({
|
||||||
|
operation: 'remove',
|
||||||
|
payload: removedObject
|
||||||
|
});
|
||||||
return removedObject;
|
return removedObject;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -6,9 +6,8 @@ import * as smartmatch from '@pushrocks/smartmatch';
|
|||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
import * as smartrx from '@pushrocks/smartrx';
|
import * as smartrx from '@pushrocks/smartrx';
|
||||||
import * as smarttime from '@pushrocks/smarttime';
|
import * as smarttime from '@pushrocks/smarttime';
|
||||||
import * as smartunique from '@pushrocks/smartunique';
|
|
||||||
|
|
||||||
export { smartdelay, smartmatch, smartpromise, smartrx, smarttime, smartunique };
|
export { smartdelay, smartmatch, smartpromise, smartrx, smarttime };
|
||||||
|
|
||||||
// ==============
|
// ==============
|
||||||
// third party
|
// third party
|
||||||
|
@ -4,7 +4,7 @@ import * as plugins from './lik.plugins';
|
|||||||
* allows you to easily keep track of a bunch of strings
|
* allows you to easily keep track of a bunch of strings
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export type TTriggerFunction = (stringArray?: string[]) => boolean;
|
export type TTriggerFunction = (stringArray?: string[]) => boolean;
|
||||||
|
|
||||||
export class Stringmap {
|
export class Stringmap {
|
||||||
private _stringArray: string[] = [];
|
private _stringArray: string[] = [];
|
||||||
@ -111,7 +111,7 @@ export class Stringmap {
|
|||||||
* notifies triggers
|
* notifies triggers
|
||||||
*/
|
*/
|
||||||
private notifyTrigger() {
|
private notifyTrigger() {
|
||||||
const filteredArray = this._triggerUntilTrueFunctionArray.filter(functionArg => {
|
const filteredArray = this._triggerUntilTrueFunctionArray.filter((functionArg) => {
|
||||||
return !functionArg();
|
return !functionArg();
|
||||||
});
|
});
|
||||||
this._triggerUntilTrueFunctionArray = filteredArray;
|
this._triggerUntilTrueFunctionArray = filteredArray;
|
||||||
|
@ -62,15 +62,15 @@ export class Tree<T> {
|
|||||||
return this.symbolTree.ancestorsToArray(objectArg, optionsArg);
|
return this.symbolTree.ancestorsToArray(objectArg, optionsArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
treeToArray(rootArg, optionsArg: any): T[] {
|
treeToArray(rootArg: T, optionsArg: any): T[] {
|
||||||
return this.symbolTree.treeToArray(rootArg, optionsArg);
|
return this.symbolTree.treeToArray(rootArg, optionsArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
childrenIterator(parentArg: T, optionsArg): T {
|
childrenIterator(parentArg: T, optionsArg: any): T {
|
||||||
return this.symbolTree.childrenIterator(parentArg, optionsArg);
|
return this.symbolTree.childrenIterator(parentArg, optionsArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
previousSiblingsIterator(objectArg): T {
|
previousSiblingsIterator(objectArg: T): T {
|
||||||
return this.symbolTree.previousSiblingsIterator(objectArg);
|
return this.symbolTree.previousSiblingsIterator(objectArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,11 +78,11 @@ export class Tree<T> {
|
|||||||
return this.symbolTree.nextSiblingsIterator();
|
return this.symbolTree.nextSiblingsIterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
ancestorsIterator(objectArg) {
|
ancestorsIterator(objectArg: T) {
|
||||||
this.symbolTree.ancestorsIterator();
|
this.symbolTree.ancestorsIterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
treeIterator(rootArg: T, optionsArg): Iterable<T> {
|
treeIterator(rootArg: T, optionsArg: any): Iterable<T> {
|
||||||
return this.symbolTree.treeIterator(rootArg);
|
return this.symbolTree.treeIterator(rootArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +114,7 @@ export class Tree<T> {
|
|||||||
return this.symbolTree.prependChild(referenceObjectArg, newObjectArg);
|
return this.symbolTree.prependChild(referenceObjectArg, newObjectArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
appendChild(referenceObjectArg, newObjectArg) {
|
appendChild(referenceObjectArg: T, newObjectArg: T) {
|
||||||
return this.symbolTree.appendChild(referenceObjectArg, newObjectArg);
|
return this.symbolTree.appendChild(referenceObjectArg, newObjectArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +126,7 @@ export class Tree<T> {
|
|||||||
* returns a branch of the tree as JSON
|
* returns a branch of the tree as JSON
|
||||||
* can be user
|
* can be user
|
||||||
*/
|
*/
|
||||||
toJsonWithHierachy(rootElement) {
|
toJsonWithHierachy(rootElement: T) {
|
||||||
const treeIterable = this.treeIterator(rootElement, {});
|
const treeIterable = this.treeIterator(rootElement, {});
|
||||||
for (const treeItem of treeIterable) {
|
for (const treeItem of treeIterable) {
|
||||||
console.log(treeItem);
|
console.log(treeItem);
|
||||||
@ -137,5 +137,5 @@ export class Tree<T> {
|
|||||||
* builds a tree from a JSON with hierachy
|
* builds a tree from a JSON with hierachy
|
||||||
* @param rootElement
|
* @param rootElement
|
||||||
*/
|
*/
|
||||||
fromJsonWithHierachy(rootElement) {}
|
fromJsonWithHierachy(rootElement: T) {}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user