diff --git a/src/replacements.ts b/src/replacements.ts index 93a83ed..a6ccb5d 100644 --- a/src/replacements.ts +++ b/src/replacements.ts @@ -1,47 +1,11 @@ export const defaultPolyfills: Record> = { // Micro-utilities - 'is-number': { - default: `v => typeof v === 'number'`, - }, - 'is-plain-object': { - default: 'v => typeof v === \"object\" && v !== null && v.constructor === Object', - }, - 'is-primitve': { - default: 'v => v === null || (typeof v !== \"function\" && typeof v !== \"object\")', - }, - 'is-regexp': { - default: 'v => Object.prototype.toString.call(v) === "[object RegExp]"', - }, - 'is-travis': { - default: '() => \"TRAVIS\" in process.env', - }, - 'is-npm': { - default: '() => process.env.npm_config_user_agent?.startsWith(\"npm\")', + 'call-bind': { + default: 'v => Function.call.bind(v)', }, 'clone-regexp': { default: 'v => new RegExp(v)', }, - 'split-lines': { - default: 'str => str.split(/\\r?\\n/)', - }, - 'is-windows': { - default: '() => process.platform === \"win32\"', - }, - 'is-whitespace': { - default: 'str => str.trim() === \"\"', - }, - 'is-string': { - default: `v => typeof v === 'string'`, - }, - 'is-odd': { - default: 'n => (n % 2) === 1', - }, - 'is-even': { - default: 'n => (n % 2) === 0', - }, - 'call-bind': { - default: 'v => Function.call.bind(v)', - }, 'es-get-iterator': { default: 'v => v[Symbol.iterator]?.()', }, @@ -57,81 +21,117 @@ export const defaultPolyfills: Record> = { 'is-date-object': { default: 'v => Object.prototype.toString.call(v) === "[object Date]"', }, + 'is-even': { + default: 'n => (n % 2) === 0', + }, 'is-negative-zero': { default: 'v => Object.is(v, -0)', }, + 'is-npm': { + default: '() => process.env.npm_config_user_agent?.startsWith("npm")', + }, + 'is-number': { + default: 'v => typeof v === "number"', + }, 'is-number-object': { default: 'v => Object.prototype.toString.call(v) === "[object Number]"', }, + 'is-odd': { + default: 'n => (n % 2) === 1', + }, + 'is-plain-object': { + default: 'v => typeof v === "object" && v !== null && v.constructor === Object', + }, 'is-primitive': { default: 'v => v === null || (typeof v !== "function" && typeof v !== "object")', }, + 'is-primitve': { + default: 'v => v === null || (typeof v !== "function" && typeof v !== "object")', + }, + 'is-regexp': { + default: 'v => Object.prototype.toString.call(v) === "[object RegExp]"', + }, + 'is-string': { + default: 'v => typeof v === "string"', + }, + 'is-travis': { + default: '() => "TRAVIS" in process.env', + }, + 'is-whitespace': { + default: 'str => str.trim() === ""', + }, + 'is-windows': { + default: '() => process.platform === "win32"', + }, + 'split-lines': { + default: 'str => str.split(/\\r?\\n/)', + }, // native replacements - 'object.entries': { - default: 'Object.entries', + 'array-every': { + default: 'Array.prototype.every', }, - 'date': { - default: 'Date', + 'array-map': { + default: 'Array.prototype.map', + }, + 'array.from': { + default: 'Array.from', }, 'array.of': { default: 'Array.of', }, - 'number.isnan': { - default: 'Number.isNaN', + 'array.prototype.find': { + default: 'Array.prototype.find', }, 'array.prototype.findindex': { default: 'Array.prototype.findIndex', }, - 'array.from': { - default: 'Array.from', + 'date': { + default: 'Date', }, - 'object-is': { - default: 'Object.is', + 'define-properties': { + default: 'Object.defineProperties', }, - 'hasown': { - default: '(obj, prop) => obj.hasOwnProperty(prop)', + 'filter-array': { + default: 'Array.prototype.filter', + }, + 'function-bind': { + default: 'Function.prototype.bind', }, 'has-own-prop': { default: 'Object.hasOwn ?? ((o, p) => Object.prototype.hasOwnProperty.call(o, p))', }, - 'array-map': { - default: 'Array.prototype.map', + 'hasown': { + default: '(obj, prop) => obj.hasOwnProperty(prop)', + }, + 'index-of': { + default: 'Array.prototype.indexOf', }, 'is-nan': { default: 'Number.isNaN', }, - 'function-bind': { - default: 'Function.prototype.bind', + 'last-index-of': { + default: 'Array.prototype.lastIndexOf', }, - 'regexp.prototype.flags': { - default: 'RegExp.prototype.flags', + 'left-pad': { + default: 'String.prototype.padStart', }, - 'array.prototype.find': { - default: 'Array.prototype.find', + 'number.isnan': { + default: 'Number.isNaN', + }, + 'object-is': { + default: 'Object.is', }, 'object-keys': { default: 'Object.keys', }, - 'define-properties': { - default: 'Object.defineProperties', - }, - 'left-pad': { - default: 'String.prototype.padStart', + 'object.entries': { + default: 'Object.entries', }, 'pad-left': { default: 'String.prototype.padStart', }, - 'filter-array': { - default: 'Array.prototype.filter', - }, - 'array-every': { - default: 'Array.prototype.every', - }, - 'index-of': { - default: 'Array.prototype.indexOf', - }, - 'last-index-of': { - default: 'Array.prototype.lastIndexOf', + 'regexp.prototype.flags': { + default: 'RegExp.prototype.flags', }, // https://github.com/esm-dev/esm.sh/blob/main/server/embed/polyfills 'abort-controller': { @@ -157,4 +157,95 @@ export const defaultPolyfills: Record> = { 'object-assign': { default: 'Object.assign', }, + // https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore/blob/master/lib/rules/rules.json + 'lodash.capitalize': { + default: 'string.charAt(0).toUpperCase() + string.slice(1).toLowerCase()', + }, + 'lodash.castArray': { + default: 'Array.isArray(arr) ? arr : [arr]', + }, + 'lodash.cloneDeep': { + default: 'structuredClone()', + }, + 'lodash.concat': { + default: 'Array.prototype.concat', + }, + 'lodash.defaults': { + default: 'Object.assign({}, defaultValues, newValues)', + }, + 'lodash.detect': { + default: 'Array.prototype.find', + }, + 'lodash.drop': { + default: '(arr, n = 1) => arr.slice(n)', + }, + 'lodash.dropRight': { + default: '(arr, n = 1) => arr.slice(0, -n || arr.length)', + }, + 'lodash.endsWith': { + default: 'String.prototype.endsWith', + }, + 'lodash.fill': { + default: 'Array.prototype.fill', + }, + 'lodash.first': { + default: 'arr => arr.at(0) || arr[0]', + }, + 'lodash.flatten': { + default: 'Array.prototype.reduce((a, b) => a.concat(b), [])', + }, + 'lodash.head': { + default: 'Array.prototype.at(0)', + }, + 'lodash.isArrayBuffer': { + default: 'value instanceof ArrayBuffer', + }, + 'lodash.isDate': { + default: 'String.prototype.toString.call()', + }, + 'lodash.isFunction': { + default: 'typeof func === "function"', + }, + 'lodash.isString': { + default: 'str != null && typeof str.valueOf() === "string"', + }, + 'lodash.join': { + default: 'Array.prototype.join', + }, + 'lodash.last': { + default: 'arr => arr.at(-1) || arr[arr.length - 1]', + }, + 'lodash.omit': { + default: '{a, b, c, ...notOmittedValues}', + }, + 'lodash.padEnd': { + default: 'String.prototype.padEnd', + }, + 'lodash.repeat': { + default: 'String.prototype.repeat', + }, + 'lodash.replace': { + default: 'String.prototype.replace', + }, + 'lodash.reverse': { + default: 'Array.prototype.reverse', + }, + 'lodash.split': { + default: 'String.prototype.split', + }, + 'lodash.startsWith': { + default: 'String.prototype.startsWith', + }, + 'lodash.toLower': { + default: 'String.prototype.toLowerCase', + }, + 'lodash.toUpper': { + default: 'String.prototype.toUpperCase', + }, + 'lodash.trim': { + default: 'String.prototype.trim', + }, + 'lodash.uniq': { + default: '[... new Set(arr)]', + }, } diff --git a/test/__snapshots__/e2e.output.js b/test/__snapshots__/e2e.output.js index 03843cc..06c3ddd 100644 --- a/test/__snapshots__/e2e.output.js +++ b/test/__snapshots__/e2e.output.js @@ -1,4 +1,4 @@ -var isString = v => typeof v === 'string'; +var isString = v => typeof v === "string"; console.log(isString("")); diff --git a/test/index.test.ts b/test/index.test.ts index 5abb4da..1460f68 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -10,14 +10,14 @@ describe('unplugin-purge-polyfills', () => { const isString = require("is-string"); isString("am I?") `)).toMatchInlineSnapshot(` - "const isString = v => typeof v === 'string'; + "const isString = v => typeof v === "string"; isString("am I?")" `) expect(await transform(` import isString from "is-string"; isString("am I?"); `)).toMatchInlineSnapshot(` - "const isString = v => typeof v === 'string'; + "const isString = v => typeof v === "string"; isString("am I?");" `) }) @@ -32,7 +32,7 @@ describe('unplugin-purge-polyfills', () => { it('resolves polyfill imports', async () => { const code = await load('is-string') expect(code).toMatchInlineSnapshot(` - "export default v => typeof v === 'string' + "export default v => typeof v === "string" " `) })