@antoniovdlc/sort
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -1,79 +0,55 @@ | ||
declare module "types" { | ||
export type Compare = string | number | Date | Record<string | number | symbol, any>; | ||
export type CompareFunction = (a: Compare, b: Compare, direction?: number) => number; | ||
type SortFunctionAscDesc = CompareFunction & { | ||
asc: CompareFunction; | ||
desc: CompareFunction; | ||
}; | ||
export type SortFunction = SortFunctionAscDesc & { | ||
by: (key: string) => SortFunctionAscDesc; | ||
}; | ||
} | ||
declare module "create-compare-fn" { | ||
import type { CompareFunction, SortFunction } from "types"; | ||
/** | ||
* Utility function to create custom compare function for sorting Arrays | ||
* @param coerceType Function to coerce the types of values | ||
* @param compareFn Compare function to apply to the values | ||
* @returns Custom compare function | ||
*/ | ||
function createCompareFunction(coerceType?: Function, compareFn?: CompareFunction): SortFunction; | ||
export default createCompareFunction; | ||
} | ||
declare module "index" { | ||
import type { CompareFunction } from "types"; | ||
import createCompareFunction from "create-compare-fn"; | ||
/** | ||
* Combine multipe sorting functions in order. | ||
* | ||
* Example: | ||
* [ | ||
* { name: "Alice", age: 32 }, | ||
* { name: "Bob", age: 45 }, | ||
* { name: "Alice", age: 28 }, | ||
* ].sort(combine([ | ||
* alphabetically.by("name"), | ||
* numerically.by("age").desc | ||
* ])) | ||
* => | ||
* [ | ||
* { name: "Alice", age: 32 }, | ||
* { name: "Alice", age: 28 }, | ||
* { name: "Bob", age: 45 }, | ||
* ] | ||
* | ||
* @param sortFns List of sorting functions | ||
* @returns | ||
*/ | ||
function combine(sortFns: Array<CompareFunction>): CompareFunction; | ||
/** | ||
* Sorts arrays alphabetically (taking into account case) | ||
* | ||
* Example: ["bob", "Alice", "tom", "Candice"].sort(alphabetically) | ||
* => ["Alice", "Candice", "bob", "tom"] | ||
*/ | ||
const alphabetically: import("types").SortFunction; | ||
/** | ||
* Sorts arrays alphabetically (regardless of case) | ||
* | ||
* Example: ["bob", "Alice", "tom", "Candice"].sort(alphabeticallyBase) | ||
* => ["Alice", "bob", "Candice", "tom"] | ||
*/ | ||
const alphabeticallyBase: import("types").SortFunction; | ||
/** | ||
* Sorts arrays chronologically | ||
* | ||
* Example: ["01-01-2001", "01-01-1970", "02-02-2002"].sort(chronologically) | ||
* => ["01-01-1970", "01-01-2001", "02-02-2002"] | ||
*/ | ||
const chronologically: import("types").SortFunction; | ||
/** | ||
* Sorts arrays numerically | ||
* | ||
* Example: [1, 2, 23, 30, 4].sort(numerically) | ||
* => [1, 2, 4, 23, 30] | ||
*/ | ||
const numerically: import("types").SortFunction; | ||
export { combine, createCompareFunction }; | ||
export { alphabetically, alphabeticallyBase, chronologically, numerically }; | ||
} | ||
import type { CompareFunction } from "./types"; | ||
import createCompareFunction from "./create-compare-fn"; | ||
/** | ||
* Combine multipe sorting functions in order. | ||
* | ||
* Example: | ||
* [ | ||
* { name: "Alice", age: 32 }, | ||
* { name: "Bob", age: 45 }, | ||
* { name: "Alice", age: 28 }, | ||
* ].sort(combine([ | ||
* alphabetically.by("name"), | ||
* numerically.by("age").desc | ||
* ])) | ||
* => | ||
* [ | ||
* { name: "Alice", age: 32 }, | ||
* { name: "Alice", age: 28 }, | ||
* { name: "Bob", age: 45 }, | ||
* ] | ||
* | ||
* @param sortFns List of sorting functions | ||
* @returns | ||
*/ | ||
declare function combine(sortFns: Array<CompareFunction>): CompareFunction; | ||
/** | ||
* Sorts arrays alphabetically (taking into account case) | ||
* | ||
* Example: ["bob", "Alice", "tom", "Candice"].sort(alphabetically) | ||
* => ["Alice", "Candice", "bob", "tom"] | ||
*/ | ||
declare const alphabetically: import("./types").SortFunction; | ||
/** | ||
* Sorts arrays alphabetically (regardless of case) | ||
* | ||
* Example: ["bob", "Alice", "tom", "Candice"].sort(alphabeticallyBase) | ||
* => ["Alice", "bob", "Candice", "tom"] | ||
*/ | ||
declare const alphabeticallyBase: import("./types").SortFunction; | ||
/** | ||
* Sorts arrays chronologically | ||
* | ||
* Example: ["01-01-2001", "01-01-1970", "02-02-2002"].sort(chronologically) | ||
* => ["01-01-1970", "01-01-2001", "02-02-2002"] | ||
*/ | ||
declare const chronologically: import("./types").SortFunction; | ||
/** | ||
* Sorts arrays numerically | ||
* | ||
* Example: [1, 2, 23, 30, 4].sort(numerically) | ||
* => [1, 2, 4, 23, 30] | ||
*/ | ||
declare const numerically: import("./types").SortFunction; | ||
export { combine, createCompareFunction }; | ||
export { alphabetically, alphabeticallyBase, chronologically, numerically }; |
{ | ||
"name": "@antoniovdlc/sort", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "Custom compare functions for sorting arrays.", | ||
@@ -11,3 +11,5 @@ "main": "dist/index.cjs.js", | ||
"dist/index.esm.js", | ||
"dist/index.d.ts" | ||
"dist/index.d.ts", | ||
"dist/types.d.ts", | ||
"dist/create-compare-fn.d.ts" | ||
], | ||
@@ -23,3 +25,3 @@ "scripts": { | ||
"build": "npm run build:types && npm run build:lib", | ||
"build:types": "tsc src/*.ts --declaration --emitDeclarationOnly --outFile dist/index.d.ts", | ||
"build:types": "tsc src/*.ts --declaration --emitDeclarationOnly --outDir dist", | ||
"build:lib": "rollup -c", | ||
@@ -26,0 +28,0 @@ "postversion": "git push && git push --tags" |
8
11411
82