diff --git a/packages/default-compare2/.gitignore b/packages/default-compare2/.gitignore new file mode 100644 index 0000000..a344ac1 --- /dev/null +++ b/packages/default-compare2/.gitignore @@ -0,0 +1,215 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# local env files +.env.local +.env.development.local +.env.test.local +.env.production.local + +# vercel +.vercel + +/.*/ +.idea +node_modules +~ci.list.txt +~ci.log.txt +~ci.errors.txt +*.stackdump +*.bak +*.old +package-lock.json +test/**/*.js +test/**/*.d.ts +test/*.js +test/*.d.ts +test/temp* +test/**/*.map +tests/**/*.js +tests/**/*.d.ts +tests/*.js +tests/*.d.ts +tests/temp* +tests/**/*.map +bin/*.d.ts +bin/**/*.d.ts +/packages/*/bin/*.d.ts +/packages/*/test/**/*.js +/packages/*/test/**/*.d.ts +/packages/*/test/*.js +/packages/*/test/*.d.ts +/packages/*/test/temp* +/packages/*/tests/**/*.js +/packages/*/tests/**/*.d.ts +/packages/*/tests/*.js +/packages/*/tests/*.d.ts +/packages/*/tests/temp* + +test/**/*.mjs +test/**/*.d.mts +test/*.mjs +test/*.d.mts +test/temp* +test/**/*.map +tests/**/*.mjs +tests/**/*.d.mts +tests/*.mjs +tests/*.d.mts +tests/temp* +tests/**/*.map +bin/*.d.mts +bin/**/*.d.mts +/packages/*/bin/*.d.mts +/packages/*/test/**/*.mjs +/packages/*/test/**/*.d.mts +/packages/*/test/*.mjs +/packages/*/test/*.d.mts +/packages/*/test/temp* +/packages/*/tests/**/*.mjs +/packages/*/tests/**/*.d.mts +/packages/*/tests/*.mjs +/packages/*/tests/*.d.mts +/packages/*/tests/temp* + +test/**/*.cjs +test/**/*.d.cts +test/*.cjs +test/*.d.cts +test/temp* +test/**/*.map +tests/**/*.cjs +tests/**/*.d.cts +tests/*.cjs +tests/*.d.cts +tests/temp* +tests/**/*.map +bin/*.d.cts +bin/**/*.d.cts +/packages/*/bin/*.d.cts +/packages/*/test/**/*.cjs +/packages/*/test/**/*.d.cts +/packages/*/test/*.cjs +/packages/*/test/*.d.cts +/packages/*/test/temp* +/packages/*/tests/**/*.cjs +/packages/*/tests/**/*.d.cts +/packages/*/tests/*.cjs +/packages/*/tests/*.d.cts +/packages/*/tests/temp* + +/src/**/*.d.ts +/src/**/*.js +/src/**/*.d.cts +/src/**/*.d.mts +/src/**/*.cjs +/src/**/*.mjs + +**/node_modules +*.tgz +/tsconfig.json.tpl +/.eslintrc.json.tpl +!tsconfig.json +!.eslintrc.json +yarn-error.log +*.log +.git +yarn.lock +.env.local +.env.*.local +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? +*.vue.js +*.vue.d.ts +*.vue.js.map +vue.config.d.ts +vue.config.js.map +.nyc_output +coverage +/*.tpl +!.forestry +!.vuepress +!.github +!.gitee +!.gitlab +.git + +*.tsbuildinfo +tsconfig.esm.json.tpl + +.browserslistrc +.nvmrc + +/.eslintignore +/package.d.ts +.nycrc +.mocharc.yml +#jest.config.js +node_modules/.cache +.yarn-integrity +jest.config.d.ts +jest.config.js.map +/report.*.json +*.js.map +*.cjs.map +*.mjs.map +*.ts.map +*.cts.map +*.mts.map +/now.json +.nyc_output +.coverage-cache +.reify-cache +*.spec.d.ts +*.spec.js +.nowignore +*/**/.github +!/.github +!npm-shrinkwrap.json +*.stat +.vercel +/test/*.tpl +/*.tpl +/report.json + +.opt-in +.opt-out +/gradle +.gradle +/out +/build + +/_*/ +_snowpack +web_modules +tsc-multi.json.tpl diff --git a/packages/default-compare2/.npmignore b/packages/default-compare2/.npmignore new file mode 100644 index 0000000..adc6f1a --- /dev/null +++ b/packages/default-compare2/.npmignore @@ -0,0 +1,124 @@ +/.pnp +.pnp.js +.idea +~ci.list.txt +~ci.log.txt +~ci.errors.txt +*.stackdump +*.bak +*.old +*.log +tsconfig.json +package-lock.json +test +.github +.gitkeep +/.*/ +/.* +tests +/~* +__test__ +__tests__ +node_modules +/node_modules/ +**/node_modules/ +*.ts +!*.d.ts +/bin/**/*.d.ts +/bin/*.d.ts + +!*.d.mts +/bin/**/*.d.mts +/bin/*.d.mts + +!*.d.cts +/bin/**/*.d.cts +/bin/*.d.cts + +/src/**/*.d.ts +/src/**/*.js +/src/**/*.d.cts +/src/**/*.d.mts +/src/**/*.cjs +/src/**/*.mjs + +*.tgz +/tsconfig.json.tpl +yarn-error.log +.git +yarn.lock +.env.local +.env.*.local +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? +*.vue.js +*.vue.d.ts +*.vue.js.map +.nyc_output +coverage +/*.tpl +webpack.config.js +vue.config.js +/jestconfig.json +/tslint.json +.git +webpack.*.config.js +webpack.*.config.d.ts +webpack.*.config.js.map +webpack.*.config.ts +karma.conf.js +/_config.yml +intellij-style-guide.xml +jest.config.js +*.tsbuildinfo +tsconfig.*.json +tsconfig.esm.json.tpl +/package.d.ts +.mocharc.yml +jest.config.js +jest.config.* +/report.*.json +now.json +/Makefile +*.spec.d.ts +*.spec.js +*.spec.ts + +*.spec.d.cts +*.spec.cjs +*.spec.cts + +*.spec.d.mts +*.spec.mjs +*.spec.mts + +*.spec.tsx +__mocks__ +__tests__ +__snapshots__ +*.snap +npm-shrinkwrap.json +/example/ +*.stat +.vercel +tsdx.config.js +/report.json + +/_*/ +_snowpack + +/snowpack.config.js +web_modules +cz-adapter + +tsc-multi.json.tpl +tsc-multi.json + +changelog-option.js diff --git a/packages/default-compare2/README.md b/packages/default-compare2/README.md new file mode 100644 index 0000000..653f3e4 --- /dev/null +++ b/packages/default-compare2/README.md @@ -0,0 +1,13 @@ +# README.md + + Basic sort algorithm that has similar behavior to Array.prototype.sort for null and undefined, but also support symbol. + +## install + +```bash +yarn add default-compare-with-symbol +yarn-tool add default-compare-with-symbol +yt add default-compare-with-symbol +``` + +- https://github.com/doowb/default-compare diff --git a/packages/default-compare2/dist/index.cjs b/packages/default-compare2/dist/index.cjs new file mode 100644 index 0000000..2142a94 --- /dev/null +++ b/packages/default-compare2/dist/index.cjs @@ -0,0 +1,8 @@ + +'use strict' + +if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') { + module.exports = require('./index.cjs.production.min.cjs') +} else { + module.exports = require('./index.cjs.development.cjs') +} diff --git a/packages/default-compare2/dist/index.cjs.development.cjs b/packages/default-compare2/dist/index.cjs.development.cjs new file mode 100644 index 0000000..8a287b6 --- /dev/null +++ b/packages/default-compare2/dist/index.cjs.development.cjs @@ -0,0 +1,96 @@ +'use strict'; + +Object.defineProperty(exports, '__esModule', { value: true }); + +function _typeOf(target) { + if (target === null) { + return 'null'; + } + + return typeof target; +} + +exports.EnumSortCompareResult = void 0; + +(function (EnumSortCompareResult) { + EnumSortCompareResult[EnumSortCompareResult["KEEP"] = 0] = "KEEP"; + EnumSortCompareResult[EnumSortCompareResult["RIGHT"] = 1] = "RIGHT"; + EnumSortCompareResult[EnumSortCompareResult["LEFT"] = -1] = "LEFT"; +})(exports.EnumSortCompareResult || (exports.EnumSortCompareResult = {})); + +function numberCompare(a, b) { + if (a < b) { + return -1; + } else if (a > b) { + return 1; + } + + return 0; +} +function nullCompare(a, b) { + const typeA = _typeOf(a); + + const typeB = _typeOf(b); + + if (typeA === 'null') { + return typeB === 'null' ? 0 : typeB === 'undefined' ? -1 : 1; + } else if (typeA === 'undefined') { + return typeB === 'null' ? 1 : typeB === 'undefined' ? 0 : 1; + } else if (typeB === 'null' || typeB === 'undefined') { + return -1; + } +} +function symbolCompare(a, b) { + const typeA = _typeOf(a); + + const typeB = _typeOf(b); + + if (typeA === 'symbol') { + return typeB === 'symbol' ? numberCompare(String(a), String(b)) : -1; + } else if (typeB === 'symbol') { + return 1; + } + + return numberCompare(a, b); +} +function dateCompare(a, b) { + return a.getTime() - b.getTime(); +} +function stringCompareLocale(a, b) { + return a.localeCompare(b); +} + +function defaultCompareBasic(a, b) { + const result = nullCompare(a, b); + + if (typeof result === 'number') { + return result; + } + + return numberCompare(a, b); +} +function defaultCompareWithSymbol(a, b) { + const result = nullCompare(a, b); + + if (typeof result === 'number') { + return result; + } + + return symbolCompare(a, b); +} +function arraySortWithSymbol(arr) { + return arr.sort(defaultCompareWithSymbol); +} + +exports._typeOf = _typeOf; +exports.arraySortWithSymbol = arraySortWithSymbol; +exports.dateCompare = dateCompare; +exports["default"] = defaultCompareWithSymbol; +exports.defaultCompare = defaultCompareWithSymbol; +exports.defaultCompareBasic = defaultCompareBasic; +exports.defaultCompareWithSymbol = defaultCompareWithSymbol; +exports.nullCompare = nullCompare; +exports.numberCompare = numberCompare; +exports.stringCompareLocale = stringCompareLocale; +exports.symbolCompare = symbolCompare; +//# sourceMappingURL=index.cjs.development.cjs.map diff --git a/packages/default-compare2/dist/index.cjs.production.min.cjs b/packages/default-compare2/dist/index.cjs.production.min.cjs new file mode 100644 index 0000000..f0ba11e --- /dev/null +++ b/packages/default-compare2/dist/index.cjs.production.min.cjs @@ -0,0 +1,2 @@ +"use strict";function _typeOf(e){return null===e?"null":typeof e}var e;function numberCompare(e,o){return eo?1:0}function nullCompare(e,o){const r=_typeOf(e),t=_typeOf(o);return"null"===r?"null"===t?0:"undefined"===t?-1:1:"undefined"===r?"null"===t?1:"undefined"===t?0:1:"null"===t||"undefined"===t?-1:void 0}function symbolCompare(e,o){const r=_typeOf(e),t=_typeOf(o);return"symbol"===r?"symbol"===t?numberCompare(String(e),String(o)):-1:"symbol"===t?1:numberCompare(e,o)}function defaultCompareWithSymbol(e,o){const r=nullCompare(e,o);return"number"==typeof r?r:symbolCompare(e,o)}Object.defineProperty(exports,"__esModule",{value:!0}),exports.EnumSortCompareResult=void 0,(e=exports.EnumSortCompareResult||(exports.EnumSortCompareResult={}))[e.KEEP=0]="KEEP",e[e.RIGHT=1]="RIGHT",e[e.LEFT=-1]="LEFT",exports._typeOf=_typeOf,exports.arraySortWithSymbol=function arraySortWithSymbol(e){return e.sort(defaultCompareWithSymbol)},exports.dateCompare=function dateCompare(e,o){return e.getTime()-o.getTime()},exports.default=defaultCompareWithSymbol,exports.defaultCompare=defaultCompareWithSymbol,exports.defaultCompareBasic=function defaultCompareBasic(e,o){const r=nullCompare(e,o);return"number"==typeof r?r:numberCompare(e,o)},exports.defaultCompareWithSymbol=defaultCompareWithSymbol,exports.nullCompare=nullCompare,exports.numberCompare=numberCompare,exports.stringCompareLocale=function stringCompareLocale(e,o){return e.localeCompare(o)},exports.symbolCompare=symbolCompare; +//# sourceMappingURL=index.cjs.production.min.cjs.map diff --git a/packages/default-compare2/dist/index.d.ts b/packages/default-compare2/dist/index.d.ts new file mode 100644 index 0000000..4630083 --- /dev/null +++ b/packages/default-compare2/dist/index.d.ts @@ -0,0 +1,24 @@ +export declare function _typeOf(target: unknown): "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "null"; +export declare const enum EnumSortCompareResult { + KEEP = 0, + RIGHT = 1, + LEFT = -1 +} +export declare function numberCompare(a: unknown, b: unknown): EnumSortCompareResult; +/** + * will return undefined when a and b not null/undefined + */ +export declare function nullCompare(a: unknown, b: unknown): EnumSortCompareResult; +export declare function symbolCompare(a: unknown, b: unknown): EnumSortCompareResult; +export declare function dateCompare(a: Date, b: Date): number; +export declare function stringCompareLocale(a: string, b: string): number; +export declare function defaultCompareBasic(a: unknown, b: unknown): EnumSortCompareResult; +export declare function defaultCompareWithSymbol(a: unknown, b: unknown): EnumSortCompareResult; +export declare function arraySortWithSymbol(arr: T): T; +export default defaultCompareWithSymbol; + +export { + defaultCompareWithSymbol as defaultCompare, +}; + +export {}; diff --git a/packages/default-compare2/dist/index.esm.mjs b/packages/default-compare2/dist/index.esm.mjs new file mode 100644 index 0000000..ef72658 --- /dev/null +++ b/packages/default-compare2/dist/index.esm.mjs @@ -0,0 +1,48 @@ +function _typeOf(e) { + return null === e ? "null" : typeof e; +} + +var e; + +function numberCompare(e, o) { + return e < o ? -1 : e > o ? 1 : 0; +} + +function nullCompare(e, o) { + const r = _typeOf(e), n = _typeOf(o); + return "null" === r ? "null" === n ? 0 : "undefined" === n ? -1 : 1 : "undefined" === r ? "null" === n ? 1 : "undefined" === n ? 0 : 1 : "null" === n || "undefined" === n ? -1 : void 0; +} + +function symbolCompare(e, o) { + const r = _typeOf(e), n = _typeOf(o); + return "symbol" === r ? "symbol" === n ? numberCompare(String(e), String(o)) : -1 : "symbol" === n ? 1 : numberCompare(e, o); +} + +function dateCompare(e, o) { + return e.getTime() - o.getTime(); +} + +function stringCompareLocale(e, o) { + return e.localeCompare(o); +} + +function defaultCompareBasic(e, o) { + const r = nullCompare(e, o); + return "number" == typeof r ? r : numberCompare(e, o); +} + +function defaultCompareWithSymbol(e, o) { + const r = nullCompare(e, o); + return "number" == typeof r ? r : symbolCompare(e, o); +} + +function arraySortWithSymbol(e) { + return e.sort(defaultCompareWithSymbol); +} + +!function(e) { + e[e.KEEP = 0] = "KEEP", e[e.RIGHT = 1] = "RIGHT", e[e.LEFT = -1] = "LEFT"; +}(e || (e = {})); + +export { e as EnumSortCompareResult, _typeOf, arraySortWithSymbol, dateCompare, defaultCompareWithSymbol as default, defaultCompareWithSymbol as defaultCompare, defaultCompareBasic, defaultCompareWithSymbol, nullCompare, numberCompare, stringCompareLocale, symbolCompare }; +//# sourceMappingURL=index.esm.mjs.map diff --git a/packages/default-compare2/dist/index.umd.development.cjs b/packages/default-compare2/dist/index.umd.development.cjs new file mode 100644 index 0000000..b6e0d9b --- /dev/null +++ b/packages/default-compare2/dist/index.umd.development.cjs @@ -0,0 +1,102 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.index = {})); +})(this, (function (exports) { 'use strict'; + + function _typeOf(target) { + if (target === null) { + return 'null'; + } + + return typeof target; + } + + exports.EnumSortCompareResult = void 0; + + (function (EnumSortCompareResult) { + EnumSortCompareResult[EnumSortCompareResult["KEEP"] = 0] = "KEEP"; + EnumSortCompareResult[EnumSortCompareResult["RIGHT"] = 1] = "RIGHT"; + EnumSortCompareResult[EnumSortCompareResult["LEFT"] = -1] = "LEFT"; + })(exports.EnumSortCompareResult || (exports.EnumSortCompareResult = {})); + + function numberCompare(a, b) { + if (a < b) { + return -1; + } else if (a > b) { + return 1; + } + + return 0; + } + function nullCompare(a, b) { + const typeA = _typeOf(a); + + const typeB = _typeOf(b); + + if (typeA === 'null') { + return typeB === 'null' ? 0 : typeB === 'undefined' ? -1 : 1; + } else if (typeA === 'undefined') { + return typeB === 'null' ? 1 : typeB === 'undefined' ? 0 : 1; + } else if (typeB === 'null' || typeB === 'undefined') { + return -1; + } + } + function symbolCompare(a, b) { + const typeA = _typeOf(a); + + const typeB = _typeOf(b); + + if (typeA === 'symbol') { + return typeB === 'symbol' ? numberCompare(String(a), String(b)) : -1; + } else if (typeB === 'symbol') { + return 1; + } + + return numberCompare(a, b); + } + function dateCompare(a, b) { + return a.getTime() - b.getTime(); + } + function stringCompareLocale(a, b) { + return a.localeCompare(b); + } + + function defaultCompareBasic(a, b) { + const result = nullCompare(a, b); + + if (typeof result === 'number') { + return result; + } + + return numberCompare(a, b); + } + function defaultCompareWithSymbol(a, b) { + const result = nullCompare(a, b); + + if (typeof result === 'number') { + return result; + } + + return symbolCompare(a, b); + } + function arraySortWithSymbol(arr) { + return arr.sort(defaultCompareWithSymbol); + } + + exports._typeOf = _typeOf; + exports.arraySortWithSymbol = arraySortWithSymbol; + exports.dateCompare = dateCompare; + exports["default"] = defaultCompareWithSymbol; + exports.defaultCompare = defaultCompareWithSymbol; + exports.defaultCompareBasic = defaultCompareBasic; + exports.defaultCompareWithSymbol = defaultCompareWithSymbol; + exports.nullCompare = nullCompare; + exports.numberCompare = numberCompare; + exports.stringCompareLocale = stringCompareLocale; + exports.symbolCompare = symbolCompare; + + Object.defineProperty(exports, '__esModule', { value: true }); + +})); +//# sourceMappingURL=index.umd.development.cjs.map diff --git a/packages/default-compare2/dist/index.umd.production.min.cjs b/packages/default-compare2/dist/index.umd.production.min.cjs new file mode 100644 index 0000000..84188f8 --- /dev/null +++ b/packages/default-compare2/dist/index.umd.production.min.cjs @@ -0,0 +1,2 @@ +!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o((e="undefined"!=typeof globalThis?globalThis:e||self).index={})}(this,(function(e){"use strict";function _typeOf(e){return null===e?"null":typeof e}var o;function numberCompare(e,o){return eo?1:0}function nullCompare(e,o){const t=_typeOf(e),n=_typeOf(o);return"null"===t?"null"===n?0:"undefined"===n?-1:1:"undefined"===t?"null"===n?1:"undefined"===n?0:1:"null"===n||"undefined"===n?-1:void 0}function symbolCompare(e,o){const t=_typeOf(e),n=_typeOf(o);return"symbol"===t?"symbol"===n?numberCompare(String(e),String(o)):-1:"symbol"===n?1:numberCompare(e,o)}function defaultCompareWithSymbol(e,o){const t=nullCompare(e,o);return"number"==typeof t?t:symbolCompare(e,o)}e.EnumSortCompareResult=void 0,(o=e.EnumSortCompareResult||(e.EnumSortCompareResult={}))[o.KEEP=0]="KEEP",o[o.RIGHT=1]="RIGHT",o[o.LEFT=-1]="LEFT",e._typeOf=_typeOf,e.arraySortWithSymbol=function arraySortWithSymbol(e){return e.sort(defaultCompareWithSymbol)},e.dateCompare=function dateCompare(e,o){return e.getTime()-o.getTime()},e.default=defaultCompareWithSymbol,e.defaultCompare=defaultCompareWithSymbol,e.defaultCompareBasic=function defaultCompareBasic(e,o){const t=nullCompare(e,o);return"number"==typeof t?t:numberCompare(e,o)},e.defaultCompareWithSymbol=defaultCompareWithSymbol,e.nullCompare=nullCompare,e.numberCompare=numberCompare,e.stringCompareLocale=function stringCompareLocale(e,o){return e.localeCompare(o)},e.symbolCompare=symbolCompare,Object.defineProperty(e,"__esModule",{value:!0})})); +//# sourceMappingURL=index.umd.production.min.cjs.map diff --git a/packages/default-compare2/jest.config.js b/packages/default-compare2/jest.config.js new file mode 100644 index 0000000..fbe938d --- /dev/null +++ b/packages/default-compare2/jest.config.js @@ -0,0 +1,129 @@ +// @ts-check + +/** + * @param {string} name + * @returns {string} + * @private + */ +function _requireResolve(name) +{ + let result; + + try + { + // @ts-ignore + const { requireResolveExtra, requireResolveCore } = require('@yarn-tool/require-resolve'); + + const paths = [ + requireResolveExtra('@bluelovers/tsdx').result, + requireResolveExtra('tsdx').result, + ].filter(Boolean); + + result = requireResolveCore(name, { + includeGlobal: true, + includeCurrentDirectory: true, + paths, + }) + } + catch (e) + { + + } + + result = result || require.resolve(name); + + console.info('[require.resolve]', name, '=>', result) + + return result +} + +const testExt = [ + 'ts', + 'tsx', + 'mts', + 'cts', + //'js', + //'jsx', +// 'mjs', +// 'cjs', +].join('|'); + +console.info(`jest.config`); +console.info(`- file: ${__filename}`); +console.info(`- cwd: ${process.cwd()}`); + +/** + * // @type { import('@jest/types').Config.InitialOptions } + * @type { import('ts-jest').InitialOptionsTsJest } + */ +module.exports = { + globals: { + 'ts-jest': { + //tsconfig: 'tsconfig.spec.json', + }, + }, + maxWorkers: 1, + clearMocks: true, + passWithNoTests: true, + moduleFileExtensions: [ + 'ts', + 'tsx', + 'mts', + 'cts', + 'js', + 'jsx', + 'mjs', + 'cjs', + 'json', + 'node', + ], + testEnvironment: 'node', + //testMatch: ['**/*.test.ts', '**/*.spec.ts'], + testMatch: void 0, + testRegex: [ + `\\.(tests?|spec)\\.(${testExt})$`, + `__tests__\/\.*\\.(${testExt})$`, + ], + testPathIgnorePatterns: [ + '/node_modules/', + '/__fixtures__/', + '/fixtures/', + '/__tests__/helpers/', + '/__tests__/utils/', + '__mocks__', + ], + //testRunner: 'jest-circus/runner', + setupFilesAfterEnv: [ + //"jest-chain", + //"jest-extended/all", + //"jest-extended-extra", + //"jest-num-close-with", + /** + * https://medium.com/doctolib/how-to-run-the-same-jest-test-suite-across-several-platforms-jest-os-detection-plugin-included-f8113832482b + * https://github.com/doctolib/jest-os-detection + */ + //'jest-os-detection', + ], + transform: { + '.(ts|tsx|mts|cts)$': _requireResolve('ts-jest'), + }, + verbose: true, + /** + * if didn't set `coverageProvider` to `v8` + * with `collectCoverage` `true`, nodejs debug point maybe will fail + */ + coverageProvider: 'v8', + collectCoverage: false, + coveragePathIgnorePatterns: [ + '/node_modules/', + '/__snapshots__/', + '/__tests__/', + //'**/node_modules/', + //'**/__snapshots__/', + //'**/__tests__/', + ], + /** + * https://github.com/facebook/jest/issues/9771#issuecomment-872764344 + */ + //resolver: 'jest-node-exports-resolver', +} diff --git a/packages/default-compare2/package.json b/packages/default-compare2/package.json new file mode 100644 index 0000000..b38414f --- /dev/null +++ b/packages/default-compare2/package.json @@ -0,0 +1,93 @@ +{ + "name": "default-compare-with-symbol", + "version": "1.0.0", + "description": "Basic sort algorithm that has similar behavior to Array.prototype.sort for null and undefined, but also support symbol.", + "keywords": [ + "compare", + "default", + "array", + "sort", + "comparison", + "comparer", + "comparator", + "type", + "generic", + "symbol", + "number", + "null", + "undefined", + "create-by-yarn-tool", + "create-by-tsdx" + ], + "homepage": "https://github.com/bluelovers/ws-array/tree/master/packages/default-compare2#readme", + "bugs": { + "url": "https://github.com/bluelovers/ws-array/issues" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/bluelovers/ws-array.git" + }, + "license": "ISC", + "author": "bluelovers", + "sideEffects": false, + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.esm.mjs", + "require": "./dist/index.cjs" + }, + "./package.json": "./package.json", + "./src/*": "./src/*" + }, + "main": "dist/index.cjs", + "unpkg": "dist/index.umd.production.min.cjs", + "module": "dist/index.esm.mjs", + "types": "dist/index.d.ts", + "typings": "dist/index.d.ts", + "directories": { + "test": "test" + }, + "scripts": { + "review": "yarn run review:coverage", + "review:coverage": "yarn run lint && yarn run coverage", + "review:test": "yarn run lint && yarn run test", + "coverage": "yarn run test -- --coverage", + "lint": "yarn run lint:eslint", + "lint:eslint": "ynpx eslint --ext .ts,.tsx,.mts,.cts ./", + "pretest": "echo pretest", + "test": "jest --passWithNoTests", + "test:jest": "jest --passWithNoTests", + "test:mocha": "ynpx --quiet -p ts-node -p mocha mocha -- --require ts-node/register \"!(node_modules)/**/*.{test,spec}.{ts,tsx}\"", + "test:tsdx": "ynpx @bluelovers/tsdx test --passWithNoTests", + "posttest": "yarn run build", + "build": "yarn run build:tsdx && yarn run build:dts:bundle", + "build:dts:bundle": "ynpx dts-bundle-generator -o ./dist/index.d.ts ./src/index.ts --no-banner --inline-declare-global & echo build:dts:bundle", + "build:dts:copy": "copy .\\src\\index.d.ts .\\dist\\index.d.ts & echo build:dts", + "build:dts:tsc": "yarn run build:dts:tsc:emit && yarn run build:dts:copy", + "build:dts:tsc:emit": "tsc --emitDeclarationOnly --declaration --noEmit false", + "build:microbundle": "ynpx microbundle --target node", + "build:tsdx": "ynpx @bluelovers/tsdx build --target node --name index", + "ci:install": "echo ci:install", + "ci:build": "echo ci:build", + "preversion": "echo preversion && yarn run test", + "version": "echo version", + "postversion": "echo postversion", + "prepublish": "echo prepublish", + "prepare": "echo prepare", + "prepublishOnly": "echo prepublishOnly", + "prepublishOnly:update": "yarn run ncu && yarn run sort-package-json", + "prepack": "echo prepack", + "pack": "echo pack", + "postpack": "echo postpack", + "publish": "echo publish", + "postpublish": "echo postpublish", + "postpublishOnly": "echo postpublishOnly", + "ncu": "yarn-tool ncu -u", + "sort-package-json": "yarn-tool sort", + "tsc:showConfig": "ynpx get-current-tsconfig -p" + }, + "dependencies": {}, + "devDependencies": {}, + "peerDependencies": {}, + "packageManager": "yarn@^1.22.11" +} diff --git a/packages/default-compare2/src/core.ts b/packages/default-compare2/src/core.ts new file mode 100644 index 0000000..e70f3f7 --- /dev/null +++ b/packages/default-compare2/src/core.ts @@ -0,0 +1,75 @@ +import { _typeOf } from './util'; + +export const enum EnumSortCompareResult +{ + KEEP = 0, + RIGHT = 1, + LEFT = -1, +} + +export function numberCompare(a: unknown, b: unknown) +{ + if (a < b) + { + return EnumSortCompareResult.LEFT + } + else if (a > b) + { + return EnumSortCompareResult.RIGHT + } + + return EnumSortCompareResult.KEEP +} + +/** + * will return undefined when a and b not null/undefined + */ +export function nullCompare(a: unknown, b: unknown) +{ + const typeA = _typeOf(a); + const typeB = _typeOf(b); + + if (typeA === 'null') + { + return typeB === 'null' ? EnumSortCompareResult.KEEP : (typeB === 'undefined' + ? EnumSortCompareResult.LEFT + : EnumSortCompareResult.RIGHT); + } + else if (typeA === 'undefined') + { + return typeB === 'null' ? EnumSortCompareResult.RIGHT : (typeB === 'undefined' + ? EnumSortCompareResult.KEEP + : EnumSortCompareResult.RIGHT); + } + else if (typeB === 'null' || typeB === 'undefined') + { + return EnumSortCompareResult.LEFT; + } +} + +export function symbolCompare(a: unknown, b: unknown) +{ + const typeA = _typeOf(a); + const typeB = _typeOf(b); + + if (typeA === 'symbol') + { + return typeB === 'symbol' ? numberCompare(String(a), String(b)) : EnumSortCompareResult.LEFT + } + else if (typeB === 'symbol') + { + return EnumSortCompareResult.RIGHT + } + + return numberCompare(a, b) +} + +export function dateCompare(a: Date, b: Date) +{ + return a.getTime() - b.getTime() +} + +export function stringCompareLocale(a: string, b: string) +{ + return a.localeCompare(b) +} diff --git a/packages/default-compare2/src/index.ts b/packages/default-compare2/src/index.ts new file mode 100644 index 0000000..f76eb86 --- /dev/null +++ b/packages/default-compare2/src/index.ts @@ -0,0 +1,37 @@ +import { nullCompare, numberCompare, symbolCompare } from './core'; + +export * from './util'; +export * from './core'; + +export function defaultCompareBasic(a: unknown, b: unknown) +{ + const result = nullCompare(a, b); + + if (typeof result === 'number') + { + return result + } + + return numberCompare(a, b) +} + +export function defaultCompareWithSymbol(a: unknown, b: unknown) +{ + const result = nullCompare(a, b); + + if (typeof result === 'number') + { + return result + } + + return symbolCompare(a, b) +} + +export function arraySortWithSymbol(arr: T) +{ + return arr.sort(defaultCompareWithSymbol) +} + +export { defaultCompareWithSymbol as defaultCompare } + +export default defaultCompareWithSymbol diff --git a/packages/default-compare2/src/util.ts b/packages/default-compare2/src/util.ts new file mode 100644 index 0000000..c78338a --- /dev/null +++ b/packages/default-compare2/src/util.ts @@ -0,0 +1,10 @@ + +export function _typeOf(target: unknown) +{ + if (target === null) + { + return 'null' as const + } + + return typeof target +} diff --git a/packages/default-compare2/test/__root.ts b/packages/default-compare2/test/__root.ts new file mode 100644 index 0000000..a579f05 --- /dev/null +++ b/packages/default-compare2/test/__root.ts @@ -0,0 +1,5 @@ +import { join } from "path"; + +export const __root = join(__dirname, '..'); + +export const isWin = process.platform === "win32"; diff --git a/packages/default-compare2/test/__snapshots__/check.spec.ts.snap b/packages/default-compare2/test/__snapshots__/check.spec.ts.snap new file mode 100644 index 0000000..4315bc3 --- /dev/null +++ b/packages/default-compare2/test/__snapshots__/check.spec.ts.snap @@ -0,0 +1,81 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`check.spec mixin 1`] = ` +Array [ + Object { + "name": "c", + "title": "C", + }, + Object { + "name": "a", + "title": "A", + }, + Object { + "title": "G", + }, + Object { + "name": "b", + "title": "B", + }, + Object { + "name": "d", + "title": "D", + }, + Object { + "name": null, + "title": "F", + }, + Object { + "name": "e", + "title": "E", + }, + null, + undefined, +] +`; + +exports[`check.spec mixin with symbol 1`] = ` +Array [ + Symbol(), + Symbol(Symbol.iterator), + Symbol(Symbol.iterator), + Symbol(iterator), + Object { + "name": "c", + "title": "C", + }, + Object { + "name": "a", + "title": "A", + }, + Object { + "title": "G", + }, + Object { + "name": "b", + "title": "B", + }, + Object { + "name": "d", + "title": "D", + }, + Object { + "name": null, + "title": "F", + }, + Object { + "name": "e", + "title": "E", + }, + "a", + "b", + "c", + "d", + "e", + "https://bafybeicm5clh7fl4up4prnbfqksou6vsp5voth54rcsxhsjysimm3o77fq.on.fleek.co/", + null, + null, + undefined, + undefined, +] +`; diff --git a/packages/default-compare2/test/check.spec.ts b/packages/default-compare2/test/check.spec.ts new file mode 100644 index 0000000..c089f89 --- /dev/null +++ b/packages/default-compare2/test/check.spec.ts @@ -0,0 +1,66 @@ +//@noUnusedParameters:false + +import { basename, extname } from 'path'; +import { defaultCompare } from '../src/index'; + +beforeAll(async () => +{ + +}); + +describe(basename(__filename, extname(__filename)), () => +{ + + it('should sort an array with null and undefined values', function () + { + const arr = ['c', 'a', undefined, 'b', 'd', null, 'e']; + expect(arr.sort(defaultCompare)).toStrictEqual(['a', 'b', 'c', 'd', 'e', null, undefined]); + }); + + test(`mixin`, () => + { + const arr = [ + { name: 'c', title: 'C' }, + undefined, + { name: 'a', title: 'A' }, + { title: 'G' }, + { name: 'b', title: 'B' }, + null, + { name: 'd', title: 'D' }, + { name: null, title: 'F' }, + { name: 'e', title: 'E' }, + ]; + + let actual = arr.sort(defaultCompare); + + expect(actual).toMatchSnapshot(); + + }); + + test(`mixin with symbol`, () => + { + const arr = [ + 'c', 'a', undefined, 'b', 'd', null, 'e', + { name: 'c', title: 'C' }, + undefined, + { name: 'a', title: 'A' }, + { title: 'G' }, + { name: 'b', title: 'B' }, + null, + { name: 'd', title: 'D' }, + { name: null, title: 'F' }, + { name: 'e', title: 'E' }, + new URL(`https://bafybeicm5clh7fl4up4prnbfqksou6vsp5voth54rcsxhsjysimm3o77fq.on.fleek.co/`), + Symbol.iterator, + Symbol('iterator'), + Symbol('Symbol.iterator'), + Symbol(), + ]; + + let actual = arr.sort(defaultCompare); + + expect(actual).toMatchSnapshot(); + + }); + +}) diff --git a/packages/default-compare2/test/tsconfig.json b/packages/default-compare2/test/tsconfig.json new file mode 100644 index 0000000..889a9a1 --- /dev/null +++ b/packages/default-compare2/test/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "module": "node16", + "noEmit": true, + "declaration": false, + "noUnusedParameters": false, + "allowUnusedLabels": true, + "noUnusedLocals": false, + "noPropertyAccessFromIndexSignature": false + } +} diff --git a/packages/default-compare2/tsconfig.json b/packages/default-compare2/tsconfig.json new file mode 100644 index 0000000..545e887 --- /dev/null +++ b/packages/default-compare2/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "@bluelovers/tsconfig/tsdx/index.json", + "compilerOptions": { + "importHelpers": true, + } +}