Skip to content

Commit

Permalink
Extract some ES6+ polyfills into a separate feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sabio committed Oct 27, 2023
1 parent 4f8f4e0 commit 16d134c
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 41 deletions.
6 changes: 5 additions & 1 deletion features.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@
},
{
"code": "POLYFILLS_FEATURE",
"desc": "Enables polyfills for some js native functions and components if they aren't present or non-native (i.g. Promise)"
"desc": "Enables polyfills for some js native functions and components if they aren't present or non-native"
},
{
"code": "POLYFILLS_ES6_FEATURE",
"desc": "Enables polyfills for ES6+ functions (e.g. Promise)."
},
{
"code": "CLICK_MAP_FEATURE",
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/watchSyncFlags/brinfoFlags/firstPaint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const getTime = (ctx: Window): number | undefined => {
const data = cFilter(
pipe(firstArg, ctxPath('name'), equal(CONTENTFUL_PAINT)),
getEntriesByType.call(ctx.performance, 'paint'),
) as { startTime: number }[];
);
if (data.length) {
return data[0].startTime;
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/array/find.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { flags } from '@inject';
import { POLYFILLS_FEATURE } from 'generated/features';
import { POLYFILLS_ES6_FEATURE } from 'generated/features';
import { toNativeOrFalse } from 'src/utils/function/isNativeFunction';
import { FindCallback, Find } from './types';

Expand All @@ -19,7 +19,7 @@ const callNativeOrPoly: Find = nativeFind
nativeFind.call(array, fn)
: findPoly;

export const cFind: Find = flags[POLYFILLS_FEATURE]
export const cFind: Find = flags[POLYFILLS_ES6_FEATURE]
? callNativeOrPoly
: <T>(fn: FindCallback<T>, array: ArrayLike<T>) =>
Array.prototype.find.call(array, fn);
11 changes: 3 additions & 8 deletions src/utils/array/includes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { flags } from '@inject';
import { POLYFILLS_FEATURE } from 'generated/features';
import { POLYFILLS_ES6_FEATURE } from 'generated/features';
import { curry2SwapArgs, equal, toNativeOrFalse } from '../function';
import { filterPoly } from './filter';
import { Includes } from './types';
Expand All @@ -16,14 +16,9 @@ const callNativeOrPoly = nativeIncludes
nativeIncludes.call(array, searchElement, fromIndex)
: includesPoly;

export const includes: Includes = flags[POLYFILLS_FEATURE]
export const includes: Includes = flags[POLYFILLS_ES6_FEATURE]
? callNativeOrPoly
: <T>(searchElement: T, array: ArrayLike<T>, fromIndex?: number) =>
Array.prototype.includes.call(array, searchElement, fromIndex);

/**
* первый аргумент где второй что
* @type function(...?): ?
*/
export const ctxIncludes: <T>(arr: ArrayLike<T>) => (el: T) => boolean =
curry2SwapArgs(includes);
export const ctxIncludes = curry2SwapArgs(includes);
10 changes: 4 additions & 6 deletions src/utils/array/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,13 @@ export const flatMap: FlatMap = flags[POLYFILLS_FEATURE]
>
).call(array, fn);

/**
* @type function(...?): ?
*/
export const ctxMap: <T, R>(cb: (e: T, i: number) => R) => (arr: T[]) => R[] =
curry2(cMap) as any;
export const ctxMap = curry2(cMap) as <T, R>(
cb: (e: T, i: number) => R,
) => (arr: T[]) => R[];

export const ctxForEach: <T, R>(
cb: (e: T, i: number) => R,
) => (arr: T[]) => void = curry2(cForEach) as any;
) => (arr: T[]) => void = curry2(cForEach);

export const ctxMapSwap = curry2SwapArgs(cMap) as <T, R>(
// eslint-disable-next-line no-use-before-define
Expand Down
9 changes: 3 additions & 6 deletions src/utils/function/isNativeFunction/toNativeOrFalse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { isNativeFunction } from 'src/utils/function/isNativeFunction/isNativeFunction';
import { F } from 'ts-toolbelt';
import { AnyFunc } from '../types';

export const toNativeOrFalse = <
P extends ReadonlyArray<any> = any,
R extends unknown = any,
>(
fn: F.Function<P, R>,
export const toNativeOrFalse = <F extends AnyFunc>(
fn: F,
functionName: string,
) => isNativeFunction(functionName, fn) && fn;
4 changes: 2 additions & 2 deletions src/utils/object/mix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { flags } from '@inject';
import { POLYFILLS_FEATURE } from 'generated/features';
import { POLYFILLS_ES6_FEATURE } from 'generated/features';
import { argsToArray } from '../function/args';
import { curry2 } from '../function/curry';
import { has } from './has';
Expand Down Expand Up @@ -28,7 +28,7 @@ export const assignPoly: typeof Object.assign = function assignPoly() {
return dst;
};

export const mix = flags[POLYFILLS_FEATURE]
export const mix = flags[POLYFILLS_ES6_FEATURE]
? Object.assign || assignPoly
: Object.assign;

Expand Down
9 changes: 4 additions & 5 deletions src/utils/object/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { flags } from '@inject';
import { POLYFILLS_FEATURE } from 'generated/features';
import { F } from 'ts-toolbelt';
import { POLYFILLS_ES6_FEATURE, POLYFILLS_FEATURE } from 'generated/features';
import { mapPoly, reducePoly } from '../array';
import { bindArg, pipe, toNativeOrFalse } from '../function';
import { isUndefined } from './assertions';
Expand Down Expand Up @@ -53,7 +52,7 @@ export const entriesPoly: Entries = <T>(obj?: Record<string, T>) => {
);
};
const callEntries =
<T>(entriesFunc: F.Function<[Record<string, T>], [string, T][]>) =>
<T>(entriesFunc: (obj: Record<string, T>) => [string, T][]) =>
(obj?: Record<string, T>) => {
if (!obj) {
return [];
Expand All @@ -65,7 +64,7 @@ const callNativeOrPolyEntries = nativeEntries
? callEntries(nativeEntries)
: entriesPoly;

export const entries: Entries = flags[POLYFILLS_FEATURE]
export const entries: Entries = flags[POLYFILLS_ES6_FEATURE]
? callNativeOrPolyEntries
: callEntries(Object.entries);

Expand All @@ -88,6 +87,6 @@ const callNativeOrPolyValues = nativeValues
? <T>(obj: Record<string, T>) => nativeValues(obj)
: valuesPoly;

export const cValues: typeof Object.values = flags[POLYFILLS_FEATURE]
export const cValues: typeof Object.values = flags[POLYFILLS_ES6_FEATURE]
? callNativeOrPolyValues
: <T>(obj: Record<string, T>) => Object.values(obj);
4 changes: 2 additions & 2 deletions src/utils/promise/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-bitwise */
// https://github.com/RubenVerborgh/promiscuous !?!?
import * as polyPromise from 'promise-polyfill';
import { POLYFILLS_FEATURE } from 'generated/features';
import { POLYFILLS_ES6_FEATURE } from 'generated/features';
import { flags } from '@inject';
import { bind } from 'src/utils/function/bind';
import { toNativeOrFalse } from 'src/utils/function/isNativeFunction/toNativeOrFalse';
Expand All @@ -10,7 +10,7 @@ import { getPath } from 'src/utils/object';
/* eslint-disable-next-line import/no-mutable-exports */
export let PolyPromise: PromiseConstructor = window.Promise;

if (flags[POLYFILLS_FEATURE]) {
if (flags[POLYFILLS_ES6_FEATURE]) {
const construct = toNativeOrFalse(PolyPromise as any, 'Promise');
const resolve = toNativeOrFalse(
getPath(PolyPromise, 'resolve')!,
Expand Down
10 changes: 2 additions & 8 deletions src/utils/string/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,5 @@ const pad = (
return start ? padding + part : part + padding;
};

type PadFunction = (
padString: string,
targetLength: number,
part: string,
) => string;

export const padStart: PadFunction = bindArg(true, pad);
export const padEnd: PadFunction = bindArg(false, pad);
export const padStart = bindArg(true, pad);
export const padEnd = bindArg(false, pad);

0 comments on commit 16d134c

Please sign in to comment.