@statoscope/helpers
Advanced tools
Comparing version 5.14.1 to 5.19.0
export declare function normalizeId<TID>(id: TID): TID | string; | ||
export declare function getIdWrapper<TID, TEntity>(getId: GetIDFn<TID, TEntity>): GetIDFn<TID | string, TEntity>; | ||
export declare type IDModifier<TID> = (id: TID) => TID; | ||
export declare function getIdWrapper<TID, TEntity>(getId: GetIDFn<TID, TEntity>, idModifier?: IDModifier<TID>): GetIDFn<TID | string, TEntity>; | ||
export declare type Entities<TEntity> = TEntity[] | Set<TEntity> | Map<unknown, TEntity> | Record<string, TEntity>; | ||
@@ -4,0 +5,0 @@ export declare type ResolverAPI = { |
@@ -8,5 +8,8 @@ "use strict"; | ||
exports.normalizeId = normalizeId; | ||
function getIdWrapper(getId) { | ||
function getIdWrapper(getId, idModifier) { | ||
return (entity) => { | ||
const id = getId(entity); | ||
let id = getId(entity); | ||
if (idModifier) { | ||
id = idModifier(id); | ||
} | ||
return normalizeId(id); | ||
@@ -13,0 +16,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import { GetIDFn } from './entity-resolver'; | ||
import { GetIDFn, IDModifier } from './entity-resolver'; | ||
export declare type IndexAPI<TID, TEntity> = { | ||
@@ -11,2 +11,6 @@ add(entity: TEntity): void; | ||
}; | ||
export default function makeIndex<TEntity, TID>(getId: GetIDFn<TID, TEntity>, source?: TEntity[]): IndexAPI<TID, TEntity>; | ||
export declare type IndexOptions<TID> = { | ||
idModifier?: IDModifier<TID>; | ||
}; | ||
export declare function sameId<TID>(id: TID): TID; | ||
export default function makeIndex<TEntity, TID>(getId: GetIDFn<TID, TEntity>, source?: TEntity[] | null, options?: IndexOptions<TID>): IndexAPI<TID, TEntity>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.sameId = void 0; | ||
const entity_resolver_1 = require("./entity-resolver"); | ||
function makeIndex(getId, source) { | ||
const wrappedGet = (0, entity_resolver_1.getIdWrapper)(getId); | ||
function sameId(id) { | ||
return id; | ||
} | ||
exports.sameId = sameId; | ||
function makeIdModifier(modifier = sameId) { | ||
const cache = new Map(); | ||
return (id) => { | ||
let cached = cache.get(id); | ||
if (cached == null) { | ||
cached = modifier(id); | ||
cache.set(id, cached); | ||
} | ||
return cached; | ||
}; | ||
} | ||
function makeIndex(getId, source, options) { | ||
const idModifier = makeIdModifier(options === null || options === void 0 ? void 0 : options.idModifier); | ||
const wrappedGet = (0, entity_resolver_1.getIdWrapper)(getId, idModifier); | ||
const storage = new Map(); | ||
@@ -15,7 +32,7 @@ const api = { | ||
hasId(id) { | ||
return storage.has((0, entity_resolver_1.normalizeId)(id)); | ||
return storage.has((0, entity_resolver_1.normalizeId)(idModifier(id))); | ||
}, | ||
get(id) { | ||
var _a; | ||
return (_a = storage.get((0, entity_resolver_1.normalizeId)(id))) !== null && _a !== void 0 ? _a : null; | ||
return (_a = storage.get((0, entity_resolver_1.normalizeId)(idModifier(id)))) !== null && _a !== void 0 ? _a : null; | ||
}, | ||
@@ -29,3 +46,3 @@ getAll() { | ||
removeById(id) { | ||
storage.delete((0, entity_resolver_1.normalizeId)(id)); | ||
storage.delete((0, entity_resolver_1.normalizeId)(idModifier(id))); | ||
}, | ||
@@ -32,0 +49,0 @@ }; |
{ | ||
"name": "@statoscope/helpers", | ||
"version": "5.14.1", | ||
"version": "5.19.0", | ||
"description": "Statoscope utils", | ||
@@ -27,3 +27,3 @@ "main": "./dist/index.js", | ||
}, | ||
"gitHead": "cd4f79745932c1a951517cc117f78afb3717f1a3" | ||
"gitHead": "e18702f042e97b40aa41b9d4b8b8e2681846f431" | ||
} |
# Helpers | ||
[![npm version](https://badge.fury.io/js/%40statoscope%2Fhelpers.svg)](https://badge.fury.io/js/%40statoscope%2Fhelpers) | ||
[![Support](https://img.shields.io/badge/-Support-blue)](https://opencollective.com/statoscope) | ||
[![Financial Contributors on Open Collective](https://opencollective.com/statoscope/all/badge.svg?label=financial+contributors)](https://opencollective.com/statoscope) | ||
Statoscope Stats Helpers |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36006
1018