@arrows/array
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -14,2 +14,6 @@ # CHANGELOG | ||
- Used `String.prototype.localeCompare` instead of manual comparison in sort methods. | ||
- Improved sorting methods for strings. | ||
## 1.2.0 | ||
- Added sorting methods with `String.prototype.localeCompare`. |
@@ -185,2 +185,4 @@ import append_ from './append_'; | ||
strDesc(a: string[]): string[]; | ||
locale(a: string[]): string[]; | ||
localeDesc(a: string[]): string[]; | ||
}; | ||
@@ -210,2 +212,10 @@ sortBy_: (<T_93, V_12>(compareFn: (a: V_12, b: V_12) => number, mappingFn: (element: T_93) => V_12, arr: T_93[]) => T_93[]) & { | ||
}; | ||
locale: { | ||
<T_96, V_15>(mappingFn: (element: T_96) => V_15): (arr: T_96[]) => T_96[]; | ||
<T_97, V_16>(mappingFn: (element: T_97) => V_16, arr: T_97[]): T_97[]; | ||
}; | ||
localeDesc: { | ||
<T_96, V_15>(mappingFn: (element: T_96) => V_15): (arr: T_96[]) => T_96[]; | ||
<T_97, V_16>(mappingFn: (element: T_97) => V_16, arr: T_97[]): T_97[]; | ||
}; | ||
}; | ||
@@ -212,0 +222,0 @@ toLocaleString: <T_98>(arr: T_98[]) => string; |
{ | ||
"name": "@arrows/array", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "license": "ISC", |
@@ -114,2 +114,4 @@ # Arrows - array | ||
- [sort.strDesc](#sort.strdesc) | ||
- [sort.locale](#sort.locale) | ||
- [sort.localeDesc](#sort.localedesc) | ||
- [sortBy\_](#sortby_) | ||
@@ -120,2 +122,4 @@ - [sortBy\_.num](#sortby_.num) | ||
- [sortBy\_.strDesc](#sortby_.strdesc) | ||
- [sortBy\_.locale](#sortby_.locale) | ||
- [sortBy\_.localeDesc](#sortby_.localedesc) | ||
- [toLocaleString](#tolocalestring) | ||
@@ -764,6 +768,4 @@ - [toString](#tostring) | ||
Sorts string arrays in an ascending order. | ||
Sorts string arrays in an ascending order using comparison operators. | ||
Uses `String.prototype.localeCompare`. | ||
#### Parameters | ||
@@ -777,6 +779,14 @@ | ||
Sorts string arrays in a descending order. | ||
Sorts string arrays in a descending order using comparison operators. | ||
Uses `String.prototype.localeCompare`. | ||
#### Parameters | ||
- `arr` Initial array | ||
**Returns:** New array | ||
### sort.locale | ||
Sorts string arrays in an ascending order using `String.prototype.localeCompare`. | ||
#### Parameters | ||
@@ -788,2 +798,12 @@ | ||
### sort.localeDesc | ||
Sorts string arrays in a descending order using `String.prototype.localeCompare`. | ||
#### Parameters | ||
- `arr` Initial array | ||
**Returns:** New array | ||
### sortBy\_ | ||
@@ -828,6 +848,4 @@ | ||
Sorts string arrays in an ascending order. | ||
Sorts string arrays in an ascending order using comparison operators. | ||
Uses `String.prototype.localeCompare`. | ||
#### Parameters | ||
@@ -842,6 +860,15 @@ | ||
Sorts string arrays in a descending order. | ||
Sorts string arrays in a descending order using comparison operators. | ||
Uses `String.prototype.localeCompare`. | ||
#### Parameters | ||
- `mappingFn` Mapping function | ||
- `arr` Initial array | ||
**Returns:** New array | ||
### sortBy\_.locale | ||
Sorts string arrays in an ascending order using `String.prototype.localeCompare`. | ||
#### Parameters | ||
@@ -854,2 +881,13 @@ | ||
### sortBy\_.localeDesc | ||
Sorts string arrays in a descending order using `String.prototype.localeCompare`. | ||
#### Parameters | ||
- `mappingFn` Mapping function | ||
- `arr` Initial array | ||
**Returns:** New array | ||
### toLocaleString | ||
@@ -856,0 +894,0 @@ |
@@ -10,2 +10,4 @@ declare type CompareFn<T> = (a: T, b: T) => number; | ||
strDesc(a: string[]): string[]; | ||
locale(a: string[]): string[]; | ||
localeDesc(a: string[]): string[]; | ||
}; | ||
@@ -22,4 +24,6 @@ /** | ||
* @method numDesc Sorts numerical arrays in a descending order | ||
* @method str Sorts string arrays in an ascending order | ||
* @method strDesc Sorts string arrays in a descending order | ||
* @method str Sorts string arrays in an ascending order using comparison operators | ||
* @method strDesc Sorts string arrays in a descending order using comparison operators | ||
* @method locale Sorts string arrays in an ascending order using localeCompare | ||
* @method localeDesc Sorts string arrays in a descending order using localeCompare | ||
*/ | ||
@@ -26,0 +30,0 @@ declare const sort: Sort; |
28
sort.js
@@ -16,4 +16,6 @@ "use strict"; | ||
* @method numDesc Sorts numerical arrays in a descending order | ||
* @method str Sorts string arrays in an ascending order | ||
* @method strDesc Sorts string arrays in a descending order | ||
* @method str Sorts string arrays in an ascending order using comparison operators | ||
* @method strDesc Sorts string arrays in a descending order using comparison operators | ||
* @method locale Sorts string arrays in an ascending order using localeCompare | ||
* @method localeDesc Sorts string arrays in a descending order using localeCompare | ||
*/ | ||
@@ -36,5 +38,10 @@ const sort = Object.assign(curriesSort, { | ||
/** | ||
* Sorts string arrays in an ascending order | ||
* Sorts string arrays in an ascending order using comparison operators. | ||
* | ||
* Uses String.prototype.localeCompare | ||
* @param arr Initial array | ||
* @returns New array | ||
*/ | ||
str: curriesSort((a, b) => (a === b ? 0 : a > b ? 1 : -1)), | ||
/** | ||
* Sorts string arrays in a descending order using comparison operators. | ||
* | ||
@@ -44,5 +51,5 @@ * @param arr Initial array | ||
*/ | ||
str: curriesSort((a, b) => a.localeCompare(b)), | ||
strDesc: curriesSort((a, b) => a === b ? 0 : a > b ? -1 : 1), | ||
/** | ||
* Sorts string arrays in a descending order | ||
* Sorts string arrays in an ascending order using `String.prototype.localeCompare`. | ||
* | ||
@@ -54,5 +61,12 @@ * Uses String.prototype.localeCompare | ||
*/ | ||
strDesc: curriesSort((a, b) => b.localeCompare(a)), | ||
locale: curriesSort((a, b) => a.localeCompare(b)), | ||
/** | ||
* Sorts string arrays in a descending order using `String.prototype.localeCompare`. | ||
* | ||
* @param arr Initial array | ||
* @returns New array | ||
*/ | ||
localeDesc: curriesSort((a, b) => b.localeCompare(a)), | ||
}); | ||
exports.sort = sort; | ||
exports.default = sort; |
@@ -23,2 +23,4 @@ declare type CompareFn<V> = (a: V, b: V) => number; | ||
strDesc: PartiallyApplied; | ||
locale: PartiallyApplied; | ||
localeDesc: PartiallyApplied; | ||
}; | ||
@@ -40,2 +42,4 @@ /** | ||
* @method strDesc Sorts string arrays in a descending order | ||
* @method locale Sorts string arrays in an ascending order using localeCompare | ||
* @method localeDesc Sorts string arrays in a descending order using localeCompare | ||
*/ | ||
@@ -42,0 +46,0 @@ declare const sortBy_: SortBy_; |
@@ -23,2 +23,4 @@ "use strict"; | ||
* @method strDesc Sorts string arrays in a descending order | ||
* @method locale Sorts string arrays in an ascending order using localeCompare | ||
* @method localeDesc Sorts string arrays in a descending order using localeCompare | ||
*/ | ||
@@ -43,6 +45,4 @@ const sortBy_ = Object.assign(curriedSortBy_, { | ||
/** | ||
* Sorts string arrays in an ascending order. | ||
* Sorts string arrays in an ascending order using comparison operators. | ||
* | ||
* Uses String.prototype.localeCompare | ||
* | ||
* @param mappingFn Mapping function | ||
@@ -54,5 +54,11 @@ * @param arr Initial array | ||
/** | ||
* Sorts string arrays in a descending order | ||
* Sorts string arrays in a descending order using comparison operators. | ||
* | ||
* Uses String.prototype.localeCompare | ||
* @param mappingFn Mapping function | ||
* @param arr Initial array | ||
* @returns New array | ||
*/ | ||
strDesc: curriedSortBy_((a, b) => b.localeCompare(a)), | ||
/** | ||
* Sorts string arrays in an ascending order using `String.prototype.localeCompare`. | ||
* | ||
@@ -63,5 +69,13 @@ * @param mappingFn Mapping function | ||
*/ | ||
strDesc: curriedSortBy_((a, b) => b.localeCompare(a)), | ||
locale: curriedSortBy_((a, b) => a.localeCompare(b)), | ||
/** | ||
* Sorts string arrays in a descending order using `String.prototype.localeCompare`. | ||
* | ||
* @param mappingFn Mapping function | ||
* @param arr Initial array | ||
* @returns New array | ||
*/ | ||
localeDesc: curriedSortBy_((a, b) => b.localeCompare(a)), | ||
}); | ||
exports.sortBy_ = sortBy_; | ||
exports.default = sortBy_; |
@@ -45,6 +45,6 @@ import { sort } from './index' | ||
it('provides a method for alphabetical sort', () => { | ||
const arr = ['foo', 'bar', 'zoo', 'baz'] | ||
const arr = ['foo', 'zoo', 'bar', 'zoo', 'baz'] | ||
const result = sort.str(arr) | ||
const expected = ['bar', 'baz', 'foo', 'zoo'] | ||
const expected = ['bar', 'baz', 'foo', 'zoo', 'zoo'] | ||
@@ -55,5 +55,23 @@ expect(result).toEqual(expected) | ||
it('provides a method for alphabetical sort - descending', () => { | ||
const arr = ['foo', 'zoo', 'bar', 'zoo', 'baz'] | ||
const result = sort.strDesc(arr) | ||
const expected = ['zoo', 'zoo', 'foo', 'baz', 'bar'] | ||
expect(result).toEqual(expected) | ||
}) | ||
it('provides a method for locale alphabetical sort', () => { | ||
const arr = ['foo', 'bar', 'zoo', 'baz'] | ||
const result = sort.strDesc(arr) | ||
const result = sort.locale(arr) | ||
const expected = ['bar', 'baz', 'foo', 'zoo'] | ||
expect(result).toEqual(expected) | ||
}) | ||
it('provides a method for locale alphabetical sort - descending', () => { | ||
const arr = ['foo', 'bar', 'zoo', 'baz'] | ||
const result = sort.localeDesc(arr) | ||
const expected = ['zoo', 'foo', 'baz', 'bar'] | ||
@@ -60,0 +78,0 @@ |
@@ -15,2 +15,4 @@ import curry from '@arrows/composition/curry' | ||
strDesc(a: string[]): string[] | ||
locale(a: string[]): string[] | ||
localeDesc(a: string[]): string[] | ||
} | ||
@@ -32,4 +34,6 @@ | ||
* @method numDesc Sorts numerical arrays in a descending order | ||
* @method str Sorts string arrays in an ascending order | ||
* @method strDesc Sorts string arrays in a descending order | ||
* @method str Sorts string arrays in an ascending order using comparison operators | ||
* @method strDesc Sorts string arrays in a descending order using comparison operators | ||
* @method locale Sorts string arrays in an ascending order using localeCompare | ||
* @method localeDesc Sorts string arrays in a descending order using localeCompare | ||
*/ | ||
@@ -52,5 +56,10 @@ const sort: Sort = Object.assign(curriesSort, { | ||
/** | ||
* Sorts string arrays in an ascending order | ||
* Sorts string arrays in an ascending order using comparison operators. | ||
* | ||
* Uses String.prototype.localeCompare | ||
* @param arr Initial array | ||
* @returns New array | ||
*/ | ||
str: curriesSort((a: string, b: string) => (a === b ? 0 : a > b ? 1 : -1)), | ||
/** | ||
* Sorts string arrays in a descending order using comparison operators. | ||
* | ||
@@ -60,5 +69,7 @@ * @param arr Initial array | ||
*/ | ||
str: curriesSort((a: string, b: string) => a.localeCompare(b)), | ||
strDesc: curriesSort((a: string, b: string) => | ||
a === b ? 0 : a > b ? -1 : 1, | ||
), | ||
/** | ||
* Sorts string arrays in a descending order | ||
* Sorts string arrays in an ascending order using `String.prototype.localeCompare`. | ||
* | ||
@@ -70,3 +81,10 @@ * Uses String.prototype.localeCompare | ||
*/ | ||
strDesc: curriesSort((a: string, b: string) => b.localeCompare(a)), | ||
locale: curriesSort((a: string, b: string) => a.localeCompare(b)), | ||
/** | ||
* Sorts string arrays in a descending order using `String.prototype.localeCompare`. | ||
* | ||
* @param arr Initial array | ||
* @returns New array | ||
*/ | ||
localeDesc: curriesSort((a: string, b: string) => b.localeCompare(a)), | ||
}) | ||
@@ -73,0 +91,0 @@ |
@@ -83,2 +83,28 @@ import { sortBy_ } from './index' | ||
}) | ||
it('provides a method for locale alphabetical sort', () => { | ||
const arr = [{ name: 'Lee' }, { name: 'Bob' }, { name: 'Joe' }] | ||
const mappingFn = (x) => x.name | ||
const result = sortBy_.locale(mappingFn, arr) | ||
const result2 = sortBy_.locale(mappingFn, arr) | ||
const expected = [{ name: 'Bob' }, { name: 'Joe' }, { name: 'Lee' }] | ||
expect(result).toEqual(result2) | ||
expect(result).toEqual(expected) | ||
}) | ||
it('provides a method for locale alphabetical sort - descending', () => { | ||
const arr = [{ name: 'Lee' }, { name: 'Bob' }, { name: 'Joe' }] | ||
const mappingFn = (x) => x.name | ||
const result = sortBy_.localeDesc(mappingFn, arr) | ||
const result2 = sortBy_.localeDesc(mappingFn, arr) | ||
const expected = [{ name: 'Lee' }, { name: 'Joe' }, { name: 'Bob' }] | ||
expect(result).toEqual(result2) | ||
expect(result).toEqual(expected) | ||
}) | ||
}) |
@@ -36,2 +36,4 @@ import curry from '@arrows/composition/curry' | ||
strDesc: PartiallyApplied | ||
locale: PartiallyApplied | ||
localeDesc: PartiallyApplied | ||
} | ||
@@ -60,2 +62,4 @@ | ||
* @method strDesc Sorts string arrays in a descending order | ||
* @method locale Sorts string arrays in an ascending order using localeCompare | ||
* @method localeDesc Sorts string arrays in a descending order using localeCompare | ||
*/ | ||
@@ -80,6 +84,4 @@ const sortBy_: SortBy_ = Object.assign(curriedSortBy_, { | ||
/** | ||
* Sorts string arrays in an ascending order. | ||
* Sorts string arrays in an ascending order using comparison operators. | ||
* | ||
* Uses String.prototype.localeCompare | ||
* | ||
* @param mappingFn Mapping function | ||
@@ -91,5 +93,11 @@ * @param arr Initial array | ||
/** | ||
* Sorts string arrays in a descending order | ||
* Sorts string arrays in a descending order using comparison operators. | ||
* | ||
* Uses String.prototype.localeCompare | ||
* @param mappingFn Mapping function | ||
* @param arr Initial array | ||
* @returns New array | ||
*/ | ||
strDesc: curriedSortBy_((a: string, b: string) => b.localeCompare(a)), | ||
/** | ||
* Sorts string arrays in an ascending order using `String.prototype.localeCompare`. | ||
* | ||
@@ -100,3 +108,11 @@ * @param mappingFn Mapping function | ||
*/ | ||
strDesc: curriedSortBy_((a: string, b: string) => b.localeCompare(a)), | ||
locale: curriedSortBy_((a: string, b: string) => a.localeCompare(b)), | ||
/** | ||
* Sorts string arrays in a descending order using `String.prototype.localeCompare`. | ||
* | ||
* @param mappingFn Mapping function | ||
* @param arr Initial array | ||
* @returns New array | ||
*/ | ||
localeDesc: curriedSortBy_((a: string, b: string) => b.localeCompare(a)), | ||
}) | ||
@@ -103,0 +119,0 @@ |
202437
5293
990