Skip to content

Commit

Permalink
refactor(shared): move defineProperties out of shared
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jul 22, 2024
1 parent 025b6dd commit 562ad30
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 52 deletions.
35 changes: 34 additions & 1 deletion packages/data/single-file/src/define-properties.ts
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
export { defineProperties as default } from '@nolyfill/shared';
const defineProperty = (object: any, name: string | number | symbol, value: any, predicate: (() => boolean) | true | undefined) => {
if (name in object) {
if (predicate === true) {
if (object[name] === value) {
return;
}
} else if (
typeof predicate !== 'function'
|| !predicate()
) {
return;
}
}
Object.defineProperty(object, name, {
configurable: true,
enumerable: false,
value,
writable: true
});
};

const defineProperties = <M extends object>(
object: object,
map: M & ThisType<any>,
predicates?: Partial<Record<keyof M, () => boolean>>
) => {
const props: Array<keyof M> = Array.prototype.concat.call(Object.keys(map), Object.getOwnPropertySymbols(map));
for (let i = 0, l = props.length; i < l; i++) {
const k = props[i];
defineProperty(object, k, map[k], predicates?.[k]);
}
};

export default defineProperties;
2 changes: 1 addition & 1 deletion packages/generated/define-properties/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/generated/define-properties/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nolyfill/define-properties",
"version": "1.0.29",
"version": "1.0.39",
"repository": {
"type": "git",
"url": "https://github.com/SukkaW/nolyfill",
Expand Down
2 changes: 1 addition & 1 deletion packages/tools/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nolyfill/shared",
"version": "1.0.38",
"version": "1.0.39",
"repository": {
"type": "git",
"url": "https://github.com/SukkaW/nolyfill",
Expand Down
31 changes: 0 additions & 31 deletions packages/tools/shared/src/define-properties.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/tools/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { uncurryThis } from './uncurry-this';
export { TypedArrayPrototype } from './typed-array-prototype';
export { defineProperties } from './define-properties';
export { makeEsShim } from './make-es-shim';
export { defineEsShim } from './define-es-shim';
36 changes: 21 additions & 15 deletions packages/tools/shared/src/make-es-shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,36 @@ export interface EsShimProp<I> {
shim(): I
}

const GET_POLYFILL = 'getPolyfill';
const SHIM = 'shim';
const IMPLEMENTATION = 'implementation';

const sharedDescriptor = {
configurable: true,
enumerable: false,
writable: true
};

const ObjectDefineProperty = Object.defineProperty;

export function makeEsShim<T extends object, I>(shim: T, implementation: I): asserts shim is T & EsShimProp<I> {
const getPolyfill = () => implementation;
if (!('implementation' in shim)) {
Object.defineProperty(shim, 'implementation', {
configurable: true,
enumerable: false,
if (!(IMPLEMENTATION in shim)) {
ObjectDefineProperty(shim, IMPLEMENTATION, {
value: implementation,
writable: true
...sharedDescriptor
});
}
if (!('getPolyfill' in shim)) {
Object.defineProperty(shim, 'getPolyfill', {
configurable: true,
enumerable: false,
if (!(GET_POLYFILL in shim)) {
ObjectDefineProperty(shim, GET_POLYFILL, {
value: getPolyfill,
writable: true
...sharedDescriptor
});
}
if (!('shim' in shim)) {
Object.defineProperty(shim, 'shim', {
configurable: true,
enumerable: false,
if (!(SHIM in shim)) {
ObjectDefineProperty(shim, SHIM, {
value: getPolyfill,
writable: true
...sharedDescriptor
});
}
}
2 changes: 1 addition & 1 deletion packages/tools/shared/src/typed-array-prototype.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype) as Uint8Array;
export const TypedArrayPrototype = Object.getPrototypeOf(Int8Array.prototype) as Int8Array;

0 comments on commit 562ad30

Please sign in to comment.