Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/remove once fn proto #456

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ Changelog

_Note: Gaps between patch versions are faulty, broken or test releases._

## v4.0.0-alpha.54 (2024-12-25)

#### :boom: Breaking Change

* Renamed `once` method in Function prototype to `memoize` `core/prelude/function/memoize`
* Renamed `once` decorator to `memoize` `core/functools/memoize`
* Removed deprecated module `core/decorators`

## v4.0.0-alpha.53 (2024-12-16)

#### :bug: Bug Fix
#### :bug: Bug Fix

* Added handling the rejection of provider in provider request engine `core/request/engines/provider`

Expand Down
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
30 changes: 0 additions & 30 deletions lib/core/decorators/index.d.ts

This file was deleted.

27 changes: 0 additions & 27 deletions lib/core/decorators/index.js

This file was deleted.

6 changes: 3 additions & 3 deletions lib/core/functools/memoize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* https://github.com/V4Fire/Core/blob/master/LICENSE
*/
/**
* Decorator for `Function.prototype.once`
* Decorator for `Function.prototype.memoize`
*
* @decorator
* @see [[Function.once]]
* @see [[Function.memoize]]
*/
export declare function once(target: object, key: string | symbol, descriptor: PropertyDescriptor): void;
export declare function memoize(target: object, key: string | symbol, descriptor: PropertyDescriptor): void;
6 changes: 3 additions & 3 deletions lib/core/functools/memoize.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.once = once;
function once(target, key, descriptor) {
exports.memoize = memoize;
function memoize(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: method.memoize()
});
return this[key](...args);
};
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;
10 changes: 5 additions & 5 deletions lib/core/prelude/function/memoize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _extend = _interopRequireDefault(require("../../../../core/prelude/extend"));
(0, _extend.default)(Function.prototype, 'once', function once() {
(0, _extend.default)(Function.prototype, 'memoize', function memoize() {
const fn = this;
let called = false,
res;
Object.defineProperty(wrapper, 'cancelOnce', {
Object.defineProperty(wrapper, 'cancelmemoize', {
configurable: true,
enumerable: false,
writable: true,
Expand All @@ -25,6 +25,6 @@ var _extend = _interopRequireDefault(require("../../../../core/prelude/extend"))
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, 'cancelMemoize', () => undefined);
(0, _extend.default)(Function, 'memoize', fn => fn.memoize());
(0, _extend.default)(Function, 'cancelMemoize', fn => fn.cancelMemoize());
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
Loading