Skip to content

Commit

Permalink
chore(build): update lib build
Browse files Browse the repository at this point in the history
  • Loading branch information
gretzkiy committed Dec 25, 2024
1 parent 60df4c3 commit 206b831
Show file tree
Hide file tree
Showing 28 changed files with 476 additions and 117 deletions.
2 changes: 1 addition & 1 deletion lib/core/async/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class Async extends _timers.default {
args
};
function handler(...handlerArgs) {
if (p.single && (hasMultipleEvent || !emitter.once)) {
if (p.single && (hasMultipleEvent || !('once' in emitter))) {
if (hasMultipleEvent) {
that.clearEventListener(ids);
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/async/events/interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface EventEmitterLike {
/**
* Extended type of event emitter
*/
export declare type EventEmitterLikeP = ((event: string, handler: Function) => CanUndef<Function>) | EventEmitterLike;
export declare type EventEmitterLikeP = ((event: string, handler: AnyFunction) => CanUndef<AnyFunction>) | EventEmitterLike;
export interface AsyncOnOptions<CTX extends object = Async> extends AsyncCbOptionsSingle<CTX> {
/**
* Additional options for the emitter
Expand Down
11 changes: 8 additions & 3 deletions lib/core/functools/memoize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
* https://github.com/V4Fire/Core/blob/master/LICENSE
*/
/**
* Decorator for `Function.prototype.once`
* Decorator for {@link once}
*
* @decorator
* @see [[Function.once]]
* @see once
*/
export declare function once(target: object, key: string | symbol, descriptor: PropertyDescriptor): void;
export declare function onceDecorator(target: object, key: string | symbol, descriptor: PropertyDescriptor): void;
/**
* Returns a new function that allows to invoke the specified function only once
* @param fn
*/
export declare function once<T extends AnyFunction>(fn: T): T;
26 changes: 24 additions & 2 deletions lib/core/functools/memoize.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,38 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.once = once;
function once(target, key, descriptor) {
exports.onceDecorator = onceDecorator;
function onceDecorator(target, key, descriptor) {
const method = descriptor.value;
if (!Object.isFunction(method)) {
throw new TypeError(`descriptor.value is not a function: ${method}`);
}
descriptor.value = function value(...args) {
Object.defineProperty(this, key, {
configurable: true,
value: method.once()
value: once(method)
});
return this[key](...args);
};
}
function once(fn) {
let called = false,
result;
Object.defineProperty(onceWrapper, 'cancelOnce', {
configurable: true,
enumerable: false,
writable: true,
value: () => {
called = true;
result = undefined;
}
});
return onceWrapper;
function onceWrapper(...args) {
if (!called) {
result = fn.apply(this, args);
called = true;
}
return result;
}
}
32 changes: 32 additions & 0 deletions lib/core/functools/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,39 @@
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _applyDecoratedDescriptor2 = _interopRequireDefault(require("@babel/runtime/helpers/applyDecoratedDescriptor"));
var _lazy = require("../../core/functools/lazy");
var _memoize = require("../../core/functools/memoize");
describe('core/functools', () => {
describe('`once`', () => {
it('should memoize the return result', () => {
const rand = (0, _memoize.once)(Math.random),
res = rand();
expect(Object.isNumber(res)).toBe(true);
expect(rand()).toBe(res);
expect(rand()).toBe(res);
});
it('should memoize the return result with different arguments', () => {
const testFn = (0, _memoize.once)(i => i);
expect(testFn(1)).toBe(1);
expect(testFn(2)).toBe(1);
});
it('should not be called multiple times', () => {
const testFn = jest.fn(i => i);
const onceFn = (0, _memoize.once)(testFn);
onceFn(1);
onceFn(2);
onceFn(3);
expect(testFn).toHaveBeenCalledTimes(1);
});
it('`cancelOnce` should reset return value', () => {
const testFn = jest.fn(i => i);
const onceFn = (0, _memoize.once)(testFn);
expect(onceFn(1)).toBe(1);
expect(onceFn(2)).toBe(1);
onceFn.cancelOnce();
expect(onceFn(1)).toBe(undefined);
expect(testFn).toBeCalledTimes(1);
});
});
describe('`@debounce`', () => {
it('should decorate a method so it runs delayed by a specified number of ms.', done => {
var _dec, _class;
Expand Down
2 changes: 1 addition & 1 deletion lib/core/log/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export * from '../../core/log/config';
* API for logging
* @defaultExport
*/
declare const logger: import("./interface").ExtendedLogger;
declare const logger: import("../../core/log/interface").ExtendedLogger;
export default logger;
2 changes: 1 addition & 1 deletion lib/core/perf/timer/engines/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
*/
export * from '../../../../core/perf/timer/engines/interface';
declare const engines: {
console: import("./interface").PerfTimerEngine;
console: import("../../../../core/perf/timer/engines/interface").PerfTimerEngine;
};
export default engines;
27 changes: 1 addition & 26 deletions lib/core/prelude/function/memoize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,4 @@

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _extend = _interopRequireDefault(require("../../../../core/prelude/extend"));
(0, _extend.default)(Function.prototype, 'once', function once() {
const fn = this;
let called = false,
res;
Object.defineProperty(wrapper, 'cancelOnce', {
configurable: true,
enumerable: false,
writable: true,
value: () => {
called = true;
res = undefined;
}
});
return wrapper;
function wrapper(...args) {
if (called) {
return res;
}
res = fn.apply(this, args);
called = true;
return res;
}
});
(0, _extend.default)(Function.prototype, 'cancelOnce', () => undefined);
(0, _extend.default)(Function, 'once', fn => fn.once());
(0, _extend.default)(Function, 'cancelOnce', fn => fn.cancelOnce());
(0, _extend.default)(Function.prototype, 'cancelOnce', () => undefined);
6 changes: 3 additions & 3 deletions lib/core/prelude/global/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ var _log = _interopRequireDefault(require("../../../core/log"));
var _extend = _interopRequireDefault(require("../../../core/prelude/extend"));
var _const = require("../../../core/prelude/global/const");
(0, _extend.default)(globalThis, 'Any', obj => obj);
(0, _extend.default)(globalThis, 'stderr', err => {
(0, _extend.default)(globalThis, 'stderr', (err, ...details) => {
if (err instanceof Object) {
if (_const.errorsToIgnore[err.type] === true) {
_log.default.info('stderr', err);
_log.default.info('stderr', err, ...details);
return;
}
_log.default.error('stderr', err);
_log.default.error('stderr', err, ...details);
}
});
9 changes: 0 additions & 9 deletions lib/core/prelude/i18n/const.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,3 @@ export declare const locale: Locale;
* The default application region
*/
export declare const region: RegionStore;
/**
* A dictionary to map literal pluralization forms to numbers
*/
export declare const pluralizeMap: Pick<{
none: number;
one: number;
some: number;
many: number;
}, "some" | "none" | "one" | "many">;
11 changes: 2 additions & 9 deletions lib/core/prelude/i18n/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.region = exports.pluralizeMap = exports.locale = exports.event = exports.emitter = void 0;
exports.region = exports.locale = exports.event = exports.emitter = void 0;
var _eventemitter = require("eventemitter2");
const emitter = new _eventemitter.EventEmitter2({
maxListeners: 100,
Expand All @@ -21,11 +21,4 @@ const region = {
value: undefined,
isDefault: false
};
exports.region = region;
const pluralizeMap = Object.createDict({
none: 0,
one: 1,
some: 2,
many: 5
});
exports.pluralizeMap = pluralizeMap;
exports.region = region;
53 changes: 38 additions & 15 deletions lib/core/prelude/i18n/helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* https://github.com/V4Fire/Core/blob/master/LICENSE
*/
import { Translation, PluralTranslation } from '../../../lang';
import type { PluralizationCount } from '../../../core/prelude/i18n/interface';
import type { I18nOpts, PluralizationCount } from '../../../core/prelude/i18n/interface';
/**
* Creates a function to internationalize strings in an application based on the given locale and keyset.
* Keyset allows you to share the same keys in different contexts.
Expand All @@ -26,40 +26,63 @@ export declare function i18nFactory(keysetNameOrNames: string | string[], custom
*
* @param value - a string for the default case, or an array of strings for the plural case
* @param params - a dictionary with parameters for internationalization
* @params [opts] - additional options for current translation
*
* @example
* ```typescript
* const example = resolveTemplate('My name is {name}, I live in {city}', {name: 'John', city: 'Denver'});
*
* console.log(example); // 'My name is John, I live in Denver'
*
* const examplePluralize = resolveTemplate([
* {count} product, // One
* {count} products, // Some
* {count} products, // Many
* {count} products, // None
* ], {count: 5});
* const examplePluralize = resolveTemplate({
* one: {count} product,
* few: {count} products,
* many: {count} products,
* zero: {count} products,
* }, {count: 5});
*
* console.log(examplePluralize); // '5 products'
* ```
*/
export declare function resolveTemplate(value: Translation, params?: I18nParams): string;
export declare function resolveTemplate(value: Translation, params?: I18nParams, opts?: I18nOpts): string;
/**
* Returns the correct plural form to translate based on the given count
*
* @param pluralTranslation - list of translation variants
* @param count - the value on the basis of which the form of pluralization will be selected
* @params [opts] - additional options for current translation
*
* @example
* ```typescript
* const result = pluralizeText([
* {count} product, // One
* {count} products, // Some
* {count} products, // Many
* {count} products, // None
* ], 5);
* const result = pluralizeText({
* one: {count} product,
* few: {count} products,
* many: {count} products,
* zero: {count} products,
* other: {count} products,
* }, 5, {pluralRules: new Intl.PluralRulse('en')});
*
* console.log(result); // '{count} products'
* ```
*/
export declare function pluralizeText(pluralTranslation: PluralTranslation, count: CanUndef<PluralizationCount>): string;
export declare function pluralizeText(pluralTranslation: PluralTranslation, count: CanUndef<PluralizationCount>, opts?: I18nOpts): string;
/**
* Returns the plural form name for a given number `n` based on the specified pluralization rules.
* Otherwise will be used default set of rules.
*
* If a `rules` object implementing `Intl.PluralRules` is provided, it will use that to determine the plural form.
* Otherwise, it will fall back to a custom rule set:
* - Returns 'zero' for `n === 0`.
* - Returns 'one' for `n === 1`.
* - Returns 'few' for `n > 1 && n < 5`.
* - Returns 'many' for all other values of `n`.
*
* @param n - The number to evaluate for pluralization.
* @param rules - Plural rules object. If undefined, a default rule set is used.
*/
export declare function getPluralFormName(n: number, rules?: CanUndef<Intl.PluralRules>): keyof Required<PluralTranslation>;
/**
* Returns an instance of `Intl.PluralRules` for a given locale, if supported.
* @param locale - The locale for which to generate plural rules.
*/
export declare function getPluralRules(locale: Language): CanUndef<Intl.PluralRules>;
Loading

0 comments on commit 206b831

Please sign in to comment.