@stackbit/utils
Advanced tools
Comparing version 0.2.4-alpha.0 to 0.2.5
@@ -29,2 +29,1 @@ /// <reference types="node" /> | ||
export declare function stringifyDataByFilePath(data: any, filePath: string): string; | ||
//# sourceMappingURL=file-utils.d.ts.map |
@@ -6,2 +6,1 @@ export * from './utils'; | ||
export { default as TaskQueue } from './task-queue'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -126,21 +126,6 @@ import { PropertyPath } from 'lodash'; | ||
*/ | ||
export declare function mapDeep(value: any, iteratee: (value: any, keyPath: (string | number)[], stack: any[]) => any, options?: { | ||
export declare function mapDeep(value: any, iteratee: (value: string, keyPath: (string | number)[], stack: any[]) => any, options?: { | ||
postOrder?: boolean; | ||
iterateCollections?: boolean; | ||
iteratePrimitives?: boolean; | ||
iterateScalars?: boolean; | ||
context?: any; | ||
}): any; | ||
export declare function asyncMapDeep(value: any, iteratee: (options: { | ||
value: any; | ||
keyPath: (string | number)[]; | ||
stack: any[]; | ||
skipNested: () => void; | ||
}) => Promise<any>, options?: { | ||
postOrder?: boolean; | ||
iterateCollections?: boolean; | ||
iteratePrimitives?: boolean; | ||
iterateScalars?: boolean; | ||
context?: any; | ||
}): Promise<any>; | ||
//# sourceMappingURL=object-utils.d.ts.map |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.asyncMapDeep = exports.mapDeep = exports.omitByNil = exports.rename = exports.copyIfNotSet = exports.copy = exports.concat = exports.prepend = exports.append = exports.getFirst = void 0; | ||
exports.mapDeep = exports.omitByNil = exports.rename = exports.copyIfNotSet = exports.copy = exports.concat = exports.prepend = exports.append = exports.getFirst = void 0; | ||
const lodash_1 = __importDefault(require("lodash")); | ||
@@ -206,3 +206,3 @@ /** | ||
if (invokeIteratee && postOrder) { | ||
value = iteratee.call(context, value, keyPath, stack); | ||
value = iteratee(value, keyPath, stack); | ||
} | ||
@@ -214,51 +214,2 @@ return value; | ||
exports.mapDeep = mapDeep; | ||
async function asyncMapDeep(value, iteratee, options = {}) { | ||
const context = lodash_1.default.get(options, 'context'); | ||
const iterateCollections = lodash_1.default.get(options, 'iterateCollections', true); | ||
const iteratePrimitives = lodash_1.default.get(options, 'iteratePrimitives', lodash_1.default.get(options, 'iterateScalars', true)); | ||
const postOrder = lodash_1.default.get(options, 'postOrder', false); | ||
async function _mapDeep(value, keyPath, stack) { | ||
const invokeIteratee = lodash_1.default.isPlainObject(value) || lodash_1.default.isArray(value) ? iterateCollections : iteratePrimitives; | ||
let shouldSkipNested = false; | ||
if (invokeIteratee && !postOrder) { | ||
value = await iteratee.call(context, { | ||
value, | ||
keyPath, | ||
stack, | ||
skipNested: () => { | ||
shouldSkipNested = true; | ||
} | ||
}); | ||
} | ||
// check if we should stop handling current branch | ||
if (shouldSkipNested) { | ||
return value; | ||
} | ||
if (lodash_1.default.isPlainObject(value)) { | ||
const mappedObject = {}; | ||
const keys = Object.keys(value); | ||
const values = await Promise.all(lodash_1.default.map(keys, (key) => { | ||
return _mapDeep(value[key], lodash_1.default.concat(keyPath, key), lodash_1.default.concat(stack, value)); | ||
})); | ||
keys.forEach((key, i) => (mappedObject[key] = values[i])); | ||
value = mappedObject; | ||
} | ||
else if (lodash_1.default.isArray(value)) { | ||
value = await Promise.all(lodash_1.default.map(value, (val, key) => { | ||
return _mapDeep(val, lodash_1.default.concat(keyPath, key), lodash_1.default.concat(stack, value)); | ||
})); | ||
} | ||
if (invokeIteratee && postOrder) { | ||
value = await iteratee.call(context, { | ||
value, | ||
keyPath, | ||
stack, | ||
skipNested: () => { } | ||
}); | ||
} | ||
return value; | ||
} | ||
return _mapDeep(value, [], []); | ||
} | ||
exports.asyncMapDeep = asyncMapDeep; | ||
//# sourceMappingURL=object-utils.js.map |
export declare function forEachPromise<T>(array: T[], callback: (value: T, index: number, array: T[]) => Promise<any>, thisArg?: any): Promise<void>; | ||
export declare function mapPromise<T, U>(array: T[], callback: (value: T, index: number, array: T[]) => Promise<U>, thisArg?: any): Promise<U[]>; | ||
export declare function mapValuesPromise<T, U>(object: T, callback: (value: T[keyof T], key: string, object: T) => Promise<U>, thisArg?: any): Promise<Record<string, U>>; | ||
export declare function reducePromise<T, U>(array: T[], callback: (accumulator: U, currentValue: T, currentIndex: number, array: T[]) => Promise<U>, initialValue: U, thisArg?: any): Promise<U>; | ||
export declare function findPromise<T>(array: T[], callback: (value: T, index: number, array: T[]) => Promise<boolean>, thisArg?: any): Promise<T | undefined>; | ||
//# sourceMappingURL=promise-utils.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.findPromise = exports.reducePromise = exports.mapValuesPromise = exports.mapPromise = exports.forEachPromise = void 0; | ||
exports.findPromise = exports.reducePromise = exports.mapPromise = exports.forEachPromise = void 0; | ||
function forEachPromise(array, callback, thisArg) { | ||
@@ -53,10 +53,2 @@ return new Promise((resolve, reject) => { | ||
exports.mapPromise = mapPromise; | ||
async function mapValuesPromise(object, callback, thisArg) { | ||
const results = {}; | ||
for (const [key, value] of Object.entries(object)) { | ||
results[key] = await callback.call(thisArg, value, key, object); | ||
} | ||
return results; | ||
} | ||
exports.mapValuesPromise = mapValuesPromise; | ||
function reducePromise(array, callback, initialValue, thisArg) { | ||
@@ -63,0 +55,0 @@ return new Promise((resolve, reject) => { |
@@ -1,20 +0,17 @@ | ||
export interface TaskQueueOptions { | ||
limit?: number; | ||
interval?: number; | ||
debug?: boolean; | ||
} | ||
export default class TaskQueue { | ||
private readonly debug; | ||
private runCount; | ||
private lastRunTime; | ||
private timeout; | ||
private taskQueue; | ||
constructor(options?: TaskQueueOptions); | ||
addTask<T>(job: () => Promise<T>, tag?: string): Promise<T>; | ||
export = TaskQueue; | ||
declare class TaskQueue { | ||
constructor(options: any); | ||
limit: any; | ||
interval: any; | ||
debug: any; | ||
runCount: number; | ||
lastRunTime: [number, number] | null; | ||
timeout: NodeJS.Timeout | null; | ||
taskQueue: any[]; | ||
_runTask(): void; | ||
addTask(job: any, tag: any): Promise<any>; | ||
clearQueue(): void; | ||
private _executeNextTask; | ||
private _runTaskLimit; | ||
private _runTaskInterval; | ||
private _runTask; | ||
_executeNextTask(): void; | ||
_runTaskLimit(runTask: any): void; | ||
_runTaskInterval(runTask: any): void; | ||
} | ||
//# sourceMappingURL=task-queue.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _ = require('lodash'); | ||
let taskTagCounter = 0; | ||
class Task { | ||
constructor(job, tag) { | ||
this.tag = tag !== null && tag !== void 0 ? tag : 'task-' + taskTagCounter++; | ||
this.tag = tag || 'task-' + taskTagCounter++; | ||
this.promise = new Promise((resolve, reject) => { | ||
@@ -14,8 +14,7 @@ this.run = function () { | ||
} | ||
class TaskQueue { | ||
module.exports = class TaskQueue { | ||
constructor(options) { | ||
var _a, _b, _c; | ||
const limit = (_a = options === null || options === void 0 ? void 0 : options.limit) !== null && _a !== void 0 ? _a : null; | ||
const interval = (_b = options === null || options === void 0 ? void 0 : options.interval) !== null && _b !== void 0 ? _b : null; | ||
this.debug = (_c = options === null || options === void 0 ? void 0 : options.debug) !== null && _c !== void 0 ? _c : false; | ||
this.limit = _.get(options, 'limit', null); | ||
this.interval = _.get(options, 'interval', null); | ||
this.debug = _.get(options, 'debug', false); | ||
this.runCount = 0; | ||
@@ -25,7 +24,7 @@ this.lastRunTime = null; | ||
this.taskQueue = []; | ||
if (interval) { | ||
this._runTask = this._runTaskInterval.bind(this, this._runTask.bind(this), interval); | ||
if (this.interval) { | ||
this._runTask = this._runTaskInterval.bind(this, this._runTask.bind(this)); | ||
} | ||
if (limit) { | ||
this._runTask = this._runTaskLimit.bind(this, this._runTask.bind(this), limit); | ||
if (this.limit) { | ||
this._runTask = this._runTaskLimit.bind(this, this._runTask.bind(this)); | ||
} | ||
@@ -50,44 +49,36 @@ } | ||
_executeNextTask() { | ||
if (this.taskQueue.length > 0) { | ||
this._runTask(); | ||
if (_.isEmpty(this.taskQueue)) { | ||
return; | ||
} | ||
this._runTask(); | ||
} | ||
_runTaskLimit(runTask, limit) { | ||
if (this.runCount < limit) { | ||
_runTaskLimit(runTask) { | ||
if (this.runCount < this.limit) { | ||
runTask(); | ||
} | ||
if (this.debug) { | ||
console.log(`[TaskQueue] task run count limit (${limit}) reached, queue size: ${this.taskQueue.length}, ` + | ||
console.log(`[TaskQueue] task run count limit (${this.limit}) reached, queue size: ${this.taskQueue.length}, ` + | ||
`running tasks: ${this.runCount}, waiting for previous tasks to finish`); | ||
} | ||
} | ||
_runTaskInterval(runTask, interval) { | ||
_runTaskInterval(runTask) { | ||
if (!this.lastRunTime) { | ||
// if this is a first task, run it immediately | ||
runTask(); | ||
} | ||
else if (!this.timeout) { | ||
// if there is no timeout, check if the time from last run is greater | ||
// than the allowed interval and run the task, otherwise set a timeout | ||
// to run the task at the right interval | ||
let diff = process.hrtime(this.lastRunTime); | ||
let diffMs = diff[0] * 1000 + diff[1] / 1000000; | ||
if (diffMs >= interval) { | ||
if (diffMs >= this.interval) { | ||
runTask(); | ||
} | ||
else { | ||
this.timeout = setTimeout(runTask, interval - diffMs); | ||
this.timeout = setTimeout(runTask, this.interval - diffMs); | ||
if (this.debug) { | ||
console.log(`[TaskQueue] task interval is less than allowed (${interval}) reached, queue size: ` + | ||
`${this.taskQueue.length}, running tasks: ${this.runCount}, waiting for ${interval - diffMs}ms`); | ||
console.log(`[TaskQueue] task interval is less than allowed (${this.interval}) reached, queue size: ` + | ||
`${this.taskQueue.length}, running tasks: ${this.runCount}, waiting for ${this.interval - diffMs}ms`); | ||
} | ||
} | ||
} | ||
// If the timeout already set, then the next call to _runTask will call | ||
// _executeNextTask at the end and will set a new timeout for the | ||
// following task. | ||
} | ||
_runTask() { | ||
// _runTask is called from _executeNextTask only when the task queue is | ||
// not empty, and timeout is set only when there is at least one task. | ||
const task = this.taskQueue.shift(); | ||
@@ -112,5 +103,3 @@ if (this.timeout) { | ||
} | ||
} | ||
exports.default = TaskQueue; | ||
; | ||
}; | ||
//# sourceMappingURL=task-queue.js.map |
export declare function fieldPathToString(fieldPath: (string | number)[]): string; | ||
export declare function failFunctionWithTag(tag: string): (message: string) => never; | ||
export declare function assertFunctionWithFail(fail: (message: string) => void): (value: any, message: string) => void; | ||
//# sourceMappingURL=utils.d.ts.map |
{ | ||
"name": "@stackbit/utils", | ||
"version": "0.2.4-alpha.0", | ||
"version": "0.2.5", | ||
"description": "Stackbit utilities", | ||
@@ -44,3 +44,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "4f0d10545fffc1b66b9774786949362ecfd06fbe" | ||
"gitHead": "2102ed599336f5b42c03e91636176d7e79b3fdde" | ||
} |
@@ -182,4 +182,4 @@ import _, { PropertyPath } from 'lodash'; | ||
value: any, | ||
iteratee: (value: any, keyPath: (string | number)[], stack: any[]) => any, | ||
options: { postOrder?: boolean; iterateCollections?: boolean; iteratePrimitives?: boolean; iterateScalars?: boolean; context?: any } = {} | ||
iteratee: (value: string, keyPath: (string | number)[], stack: any[]) => any, | ||
options: { postOrder?: boolean; iterateCollections?: boolean; iterateScalars?: boolean } = {} | ||
) { | ||
@@ -205,3 +205,3 @@ const context = _.get(options, 'context'); | ||
if (invokeIteratee && postOrder) { | ||
value = iteratee.call(context, value, keyPath, stack); | ||
value = iteratee(value, keyPath, stack); | ||
} | ||
@@ -213,59 +213,1 @@ return value; | ||
} | ||
export async function asyncMapDeep( | ||
value: any, | ||
iteratee: (options: { value: any; keyPath: (string | number)[]; stack: any[]; skipNested: () => void }) => Promise<any>, | ||
options: { postOrder?: boolean; iterateCollections?: boolean; iteratePrimitives?: boolean; iterateScalars?: boolean; context?: any } = {} | ||
) { | ||
const context = _.get(options, 'context'); | ||
const iterateCollections = _.get(options, 'iterateCollections', true); | ||
const iteratePrimitives = _.get(options, 'iteratePrimitives', _.get(options, 'iterateScalars', true)); | ||
const postOrder = _.get(options, 'postOrder', false); | ||
async function _mapDeep(value: any, keyPath: (string | number)[], stack: any[]) { | ||
const invokeIteratee = _.isPlainObject(value) || _.isArray(value) ? iterateCollections : iteratePrimitives; | ||
let shouldSkipNested = false; | ||
if (invokeIteratee && !postOrder) { | ||
value = await iteratee.call(context, { | ||
value, | ||
keyPath, | ||
stack, | ||
skipNested: () => { | ||
shouldSkipNested = true; | ||
} | ||
}); | ||
} | ||
// check if we should stop handling current branch | ||
if (shouldSkipNested) { | ||
return value; | ||
} | ||
if (_.isPlainObject(value)) { | ||
const mappedObject: Record<string, any> = {}; | ||
const keys = Object.keys(value); | ||
const values = await Promise.all( | ||
_.map(keys, (key) => { | ||
return _mapDeep(value[key], _.concat(keyPath, key), _.concat(stack, value)); | ||
}) | ||
); | ||
keys.forEach((key, i) => (mappedObject[key] = values[i])); | ||
value = mappedObject; | ||
} else if (_.isArray(value)) { | ||
value = await Promise.all( | ||
_.map(value, (val, key) => { | ||
return _mapDeep(val, _.concat(keyPath, key), _.concat(stack, value)); | ||
}) | ||
); | ||
} | ||
if (invokeIteratee && postOrder) { | ||
value = await iteratee.call(context, { | ||
value, | ||
keyPath, | ||
stack, | ||
skipNested: () => {} | ||
}); | ||
} | ||
return value; | ||
} | ||
return _mapDeep(value, [], []); | ||
} |
@@ -49,10 +49,2 @@ export function forEachPromise<T>(array: T[], callback: (value: T, index: number, array: T[]) => Promise<any>, thisArg?: any): Promise<void> { | ||
export async function mapValuesPromise<T, U>(object: T, callback: (value: T[keyof T], key: string, object: T) => Promise<U>, thisArg?: any): Promise<Record<string, U>> { | ||
const results: Record<string, U> = {}; | ||
for (const [key, value] of Object.entries(object)) { | ||
results[key] = await callback.call(thisArg, value, key, object); | ||
} | ||
return results; | ||
} | ||
export function reducePromise<T, U>( | ||
@@ -59,0 +51,0 @@ array: T[], |
@@ -15,3 +15,2 @@ { | ||
"declaration": true, | ||
"declarationMap": true, | ||
"lib": ["es2019"], | ||
@@ -18,0 +17,0 @@ "rootDir": "./src", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
77057
32
1497
1