mirror of
https://github.com/iconify/iconify.git
synced 2025-01-06 07:20:40 +00:00
Optimise API redundancy package
This commit is contained in:
parent
34d0ced20f
commit
e18b821005
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@iconify/api-redundancy",
|
||||
"description": "Reusable redundancy library for API queries",
|
||||
"version": "1.1.0",
|
||||
"version": "1.1.1",
|
||||
"author": "Vjacheslav Trushkin",
|
||||
"license": "MIT",
|
||||
"main": "dist/index.js",
|
||||
|
@ -54,38 +54,15 @@ export interface Redundancy {
|
||||
cleanup: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set configuration
|
||||
*/
|
||||
function setConfig(config: Partial<RedundancyConfig>): RedundancyConfig {
|
||||
if (
|
||||
typeof config !== 'object' ||
|
||||
typeof (config as RedundancyConfig).resources !== 'object' ||
|
||||
!((config as RedundancyConfig).resources instanceof Array) ||
|
||||
!(config as RedundancyConfig).resources.length
|
||||
) {
|
||||
throw new Error('Invalid Reduncancy configuration');
|
||||
}
|
||||
|
||||
const newConfig = Object.create(null);
|
||||
let key: keyof RedundancyConfig;
|
||||
for (key in defaultConfig) {
|
||||
if (config[key] !== void 0) {
|
||||
newConfig[key] = config[key];
|
||||
} else {
|
||||
newConfig[key] = defaultConfig[key];
|
||||
}
|
||||
}
|
||||
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Redundancy instance
|
||||
*/
|
||||
export function initRedundancy(cfg: Partial<RedundancyConfig>): Redundancy {
|
||||
// Configuration
|
||||
const config: RedundancyConfig = setConfig(cfg);
|
||||
const config: RedundancyConfig = {
|
||||
...defaultConfig,
|
||||
...cfg,
|
||||
};
|
||||
|
||||
// List of queries
|
||||
let queries: GetQueryStatus[] = [];
|
||||
@ -127,10 +104,11 @@ export function initRedundancy(cfg: Partial<RedundancyConfig>): Redundancy {
|
||||
* Find instance
|
||||
*/
|
||||
function find(callback: FilterCallback): GetQueryStatus | null {
|
||||
const result = queries.find((value) => {
|
||||
return callback(value);
|
||||
});
|
||||
return result !== void 0 ? result : null;
|
||||
return (
|
||||
queries.find((value) => {
|
||||
return callback(value);
|
||||
}) || null
|
||||
);
|
||||
}
|
||||
|
||||
// Create and return functions
|
||||
|
Loading…
Reference in New Issue
Block a user