Skip to content

Commit cc8eb30

Browse files
committed
Release version 1.2.0
Closes #3: Added `sortBy.mapAccents()`
1 parent 52b0c74 commit cc8eb30

23 files changed

+386
-441
lines changed

README.md

Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
<!-- markdownlint-disable MD033 MD034 MD014 -->
44

55
Sorts an array and allows specify multiple sorting criteria.
6-
It has support for accented characters, and also ignore case sensitive.
6+
It has support for accented characters, and ignores
7+
case sensitive to sort strings correctly.
78

89
## Content
910

1011
1. [Getting started](#getting-started)
1112
1. [Including the library](#including-the-library)
1213
1. [Examples](#examples)
13-
1. [Polyfills](#polyfills)
1414

1515
## Getting started
1616

@@ -28,17 +28,17 @@ The `sortBy` function has the following signature:
2828

2929
```javascript
3030
/**
31-
* @param {Array} array: the collection of elements to sort
32-
* @param {Function} parser: transforms each item and specifies the sorting order
31+
* @param {Array} array: the list of elements to sort
32+
* @param {Function} parser: (optional) transforms each item and specifies the sorting mode
3333
* @return {Array}
3434
*/
3535
sortBy(array: Array, parser: Function) : Array
3636
sortBy(array: Array) : Array
3737
```
3838

3939
The optional parameter `parser` is a function that transforms each element
40-
being iterated and sets the sorting rules: _ascending_ or _descending_, and
41-
the option to specify multiple fields for sorting.
40+
being iterated and sets the sorting rules: _ascending_ or _descending_.
41+
Here you can specify the way of sorting by multiple fields.
4242

4343
The `parser` callback has the following signature:
4444

@@ -52,6 +52,68 @@ parser(item: Any, index: Number) : Any
5252
parser(item: Any) : Any
5353
```
5454

55+
Also, a new static method `mapAccents` has been added to the `sortBy` function.
56+
This method allows to register a map of accents in order to sort strings correctly.
57+
58+
Signature:
59+
60+
```javascript
61+
/**
62+
* @param {String} accents: the string with the accents
63+
* @param {String} replacements: the replacement for each accent
64+
*/
65+
sortBy.mapAccents(accents: String, replacements: String) : void
66+
```
67+
68+
> Problem solved: when you try order an array of non ASCII characters like this
69+
`['é', 'a', 'ú', 'c']`, you will obtain a strange result `['c', 'e', 'á', 'ú']`.
70+
That happens because `.sort()` does not work correctly with accented characters.
71+
72+
By default `mapAccents` has an internal mapping with accents and their replacements:
73+
74+
```javascript
75+
"ÂâÀàÁáÄäÃãÅåÊêÈèÉéËëÎîÌìÍíÏïÔôÒòÓóÖöÕõÛûÙùÚúÜüÑñÝýÿ"
76+
"AaAaAaAaAaAaEeEeEeEeIiIiIiIiOoOoOoOoOoUuUuUuUuNnYyy"
77+
```
78+
79+
To register a new set of special characters you must provide their replacements:
80+
81+
```javascript
82+
// register the special characters
83+
sortBy.mapAccents(
84+
'ª@$',
85+
'aas',
86+
);
87+
88+
const arr = ['$impson', 'Cªl@bazä', 'M@ría', 'Cal@bªzA'];
89+
sortBy(arr);
90+
/**
91+
* expected:
92+
* ["Cªl@bazä", "Cal@bªzA", "M@ría", "$impson"]
93+
*
94+
* translated as:
95+
* ["CALABAZA", "CALABAZA", "MARIA", "SIMPSON"]
96+
*/
97+
98+
sortBy(arr, item => `DESC:${item}`);
99+
/**
100+
* expected:
101+
* ["$impson", "M@ría", "Cal@bªzA", "Cªl@bazä"]
102+
*
103+
* translated as:
104+
* ["SIMPSON", "MARIA", "CALABAZA", "CALABAZA"]
105+
*/
106+
```
107+
108+
In the example above, after calling `sortBy.mapAccents()` we
109+
added new accents and their replacements at the beginning of
110+
the internal mapping, honoring the user mapping first:
111+
112+
```javascript
113+
"ª@$ÂâÀàÁáÄäÃãÅåÊêÈèÉéËëÎîÌìÍíÏïÔôÒòÓóÖöÕõÛûÙùÚúÜüÑñÝýÿ"
114+
"aasAaAaAaAaAaAaEeEeEeEeIiIiIiIiOoOoOoOoOoUuUuUuUuNnYyy"
115+
```
116+
55117
[&#9751; Back to Index](#content)
56118

57119
## Including the library
@@ -61,9 +123,13 @@ parser(item: Any) : Any
61123
```html
62124
<!-- from unpkg.com -->
63125
<script src="https://unpkg.com/array-sort-by/dist/sort-by.min.js"></script>
126+
<!-- from unpkg.com, including polyfills -->
127+
<script src="https://unpkg.com/array-sort-by/dist/sort-by-all.min.js"></script>
64128

65-
<!-- or from rawgit.com -->
66-
<script src="https://cdn.rawgit.com/jherax/array-sort-by/1.1.2/dist/sort-by.min.js"></script>
129+
<!-- from rawgit.com -->
130+
<script src="https://cdn.rawgit.com/jherax/array-sort-by/1.2.0/dist/sort-by.min.js"></script>
131+
<!-- from rawgit.com, including polyfills -->
132+
<script src="https://cdn.rawgit.com/jherax/array-sort-by/1.2.0/dist/sort-by-all.min.js"></script>
67133
```
68134

69135
In the above case, the function [`sortBy`](#examples) is included as
@@ -130,10 +196,10 @@ sortBy(arr, n => -n);
130196
*/
131197
```
132198

133-
### Sorting DESC: date-strings as `Date`
199+
### Sorting DESC: date-strings as Date
134200

135201
```javascript
136-
let arr = ["1983/03/06", "1980/12/24", "1985/08/31", "1983/03/05"];
202+
let arr = ['1983/03/06', '1980/12/24', '1985/08/31', '1983/03/05'];
137203
sortBy(arr, (s) => -new Date(s));
138204

139205
/**
@@ -258,44 +324,6 @@ sortBy(arr, item => [item.name, -item.age, item.id]);
258324

259325
[&#9751; Back to Index](#content)
260326

261-
## Polyfills
262-
263-
This library is written using some of the new ES5/ES6 features. If you have
264-
to support Non-standard-compliant browsers like Internet Explorer, you can
265-
polyfill some of the missing features with the following alternatives:
266-
267-
**Using [es6-shim](https://github.com/paulmillr/es6-shim)**
268-
269-
```html
270-
<!-- put this script FIRST, before all other scripts -->
271-
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.min.js"></script>
272-
```
273-
274-
**Using [polyfill.io](https://polyfill.io/v2/docs/)**
275-
276-
```html
277-
<!-- put this script FIRST, before all other scripts -->
278-
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default-3.3"></script>
279-
```
280-
281-
[Polyfill.io](https://polyfill.io/v2/docs/examples) reads the `User-Agent`
282-
header of each request and returns the polyfills that are suitable for the
283-
requesting browser.
284-
285-
If you want to request specific polyfills, you can pass a query parameter
286-
to the url, for example:
287-
288-
```html
289-
<!--[if IE]>
290-
<script src="https://polyfill.io/v2/polyfill.min.js?features=default-3.3&flags=always"></script>
291-
<![endif]-->
292-
```
293-
294-
Read the list of available features:
295-
[Features and Browsers Supported](https://polyfill.io/v2/docs/features/).
296-
297-
[&#9751; Back to Index](#content)
298-
299327
## Versioning
300328

301329
This projects adopts the [Semantic Versioning](http://semver.org/)

dist/sort-by-full.min.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/sort-by-full.min.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)