UNPKG

2.73 kBJavaScriptView Raw
1import { compareText as _compareText } from '../../utils/string'
2import { factory } from '../../utils/factory'
3import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
4import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13'
5
6const name = 'compareText'
7const dependencies = [
8 'typed',
9 'matrix'
10]
11
12export const createCompareText = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix }) => {
13 const algorithm13 = createAlgorithm13({ typed })
14 const algorithm14 = createAlgorithm14({ typed })
15
16 /**
17 * Compare two strings lexically. Comparison is case sensitive.
18 * Returns 1 when x > y, -1 when x < y, and 0 when x == y.
19 *
20 * For matrices, the function is evaluated element wise.
21 *
22 * Syntax:
23 *
24 * math.compareText(x, y)
25 *
26 * Examples:
27 *
28 * math.compareText('B', 'A') // returns 1
29 * math.compareText('2', '10') // returns 1
30 * math.compare('2', '10') // returns -1
31 * math.compareNatural('2', '10') // returns -1
32 *
33 * math.compareText('B', ['A', 'B', 'C']) // returns [1, 0, -1]
34 *
35 * See also:
36 *
37 * equal, equalText, compare, compareNatural
38 *
39 * @param {string | Array | DenseMatrix} x First string to compare
40 * @param {string | Array | DenseMatrix} y Second string to compare
41 * @return {number | Array | DenseMatrix} Returns the result of the comparison:
42 * 1 when x > y, -1 when x < y, and 0 when x == y.
43 */
44 const compareText = typed(name, {
45
46 'any, any': _compareText,
47
48 'DenseMatrix, DenseMatrix': function (x, y) {
49 return algorithm13(x, y, _compareText)
50 },
51
52 'Array, Array': function (x, y) {
53 // use matrix implementation
54 return compareText(matrix(x), matrix(y)).valueOf()
55 },
56
57 'Array, Matrix': function (x, y) {
58 // use matrix implementation
59 return compareText(matrix(x), y)
60 },
61
62 'Matrix, Array': function (x, y) {
63 // use matrix implementation
64 return compareText(x, matrix(y))
65 },
66
67 'DenseMatrix, any': function (x, y) {
68 return algorithm14(x, y, _compareText, false)
69 },
70
71 'any, DenseMatrix': function (x, y) {
72 return algorithm14(y, x, _compareText, true)
73 },
74
75 'Array, any': function (x, y) {
76 // use matrix implementation
77 return algorithm14(matrix(x), y, _compareText, false).valueOf()
78 },
79
80 'any, Array': function (x, y) {
81 // use matrix implementation
82 return algorithm14(matrix(y), x, _compareText, true).valueOf()
83 }
84 })
85
86 return compareText
87})
88
89export const createCompareTextNumber = /* #__PURE__ */ factory(name, ['typed'], ({ typed }) => {
90 return typed(name, {
91 'any, any': _compareText
92 })
93})