mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 23:07:06 +00:00
add types for compare-func (#30630)
This commit is contained in:
committed by
Pranav Senthilnathan
parent
efb26511dd
commit
8225c94050
@@ -0,0 +1,30 @@
|
||||
import compareFunc = require("compare-func");
|
||||
|
||||
// This is taken from https://github.com/stevemao/compare-func/blob/master/README.md#usage and extended
|
||||
|
||||
// sort by an object property
|
||||
[{ x: "b" }, { x: "a" }, { x: "c" }].sort(compareFunc("x"));
|
||||
// => [{x: "a"}, {x: "b"}, {x: "c"}]
|
||||
|
||||
// sort by a nested object property
|
||||
[{ x: { y: "b" } }, { x: { y: "a" } }].sort(compareFunc("x.y"));
|
||||
// => [{x: {y: "a"}}, {x: {y: "b"}}]
|
||||
|
||||
// sort by the `x` property, then `y`
|
||||
[{ x: "c", y: "c" }, { x: "b", y: "a" }, { x: "b", y: "b" }].sort(compareFunc(["x", "y"]));
|
||||
// => [{x: "b", y: "a"}, {x: "b", y: "b"}, {x: "c", y: "c"}]
|
||||
|
||||
// sort by the returned value
|
||||
[{ x: "b" }, { x: "a" }, { x: "c" }].sort(compareFunc((el) => el.x));
|
||||
// => [{x: "a"}, {x: "b"}, {x: "c"}]
|
||||
|
||||
// sort by an array of functions
|
||||
[{ x: "c", y: "c" }, { x: "b", y: "a" }, { x: "b", y: "b" }].sort(compareFunc([
|
||||
(el) => el.x,
|
||||
(el) => el.y,
|
||||
]));
|
||||
// => [{x: "a"}, {x: "b"}, {x: "c"}]
|
||||
|
||||
// sort by itself
|
||||
[1, 3, 2].sort(compareFunc());
|
||||
// => [1, 2, 3]
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
// Type definitions for compare-func 1.3
|
||||
// Project: https://github.com/stevemao/compare-func
|
||||
// Definitions by: Dogan Fennibay <https://github.com/fennibay>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.4
|
||||
|
||||
type extractFunc<T> = (e: T) => number | string;
|
||||
|
||||
declare function compare_func<T>(prop?: ReadonlyArray<string | extractFunc<T>> | string | extractFunc<T>):
|
||||
(e1: T, e2: T) => number;
|
||||
|
||||
export = compare_func;
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"compare-func-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user