Skip to content

Commit

Permalink
Feat: Make string comparison always be case insensitive (#35)
Browse files Browse the repository at this point in the history
* fix(license): update copyright period

* feat(compare): remove option caseSensitive

* feat: make string comparison always be case insensitive

* docs: remove case sensitive examples

* chore(build): update size snapshot
  • Loading branch information
yobacca committed Jan 20, 2019
1 parent 3af4140 commit 35c9d0d
Show file tree
Hide file tree
Showing 17 changed files with 347 additions and 640 deletions.
6 changes: 3 additions & 3 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"iife/natural-orderby.min.js": {
"bundled": 18848,
"minified": 4502,
"gzipped": 1598
"bundled": 18376,
"minified": 4406,
"gzipped": 1554
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018 Olaf Ennen
Copyright (c) 2018 - present Olaf Ennen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
32 changes: 9 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,9 @@ orderBy(['$102.00', '$21.10', '$101.02', '$101.01']);
// => ['$21.10', '$101.01', '$101.02', '$102.00']


// Default sort order case-sensitive
// Case-insensitive sort order

orderBy(['A', 'b', 'C', 'd', 'E', 'f']);
// => ['A', 'C', 'E', 'b', 'd', 'f']


// Enabled case-insensitive sort order

orderBy(['A', 'C', 'E', 'b', 'd', 'f'], [v => v.toLowerCase()]);
orderBy(['A', 'C', 'E', 'b', 'd', 'f']);
// => ['A', 'b', 'C', 'd', 'E', 'f']


Expand Down Expand Up @@ -343,17 +337,15 @@ Creates a compare function that defines the natural sort order and which may be
compare(options?: CompareOptions): CompareFn
```
| Type | Value |
| :--------------- | :-------------------------------------------------------------------- |
| `CompareOptions` | <code>{ caseSensitive?: boolean, order?: 'asc' &#124; 'desc' }</code> |
| `CompareFn` | <code>(valueA: mixed, valueB: mixed) => number</code> |
| Type | Value |
| :--------------- | :---------------------------------------------------- |
| `CompareOptions` | <code>{ order?: 'asc' &#124; 'desc' }</code> |
| `CompareFn` | <code>(valueA: mixed, valueB: mixed) => number</code> |
#### Description
`compare()` returns a compare function that defines the natural sort order and which may be passed to [`Array.prototype.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort).
If `options` or its property `caseSensitive` is unspecified, values are sorted case sensitive. Otherwise, specify `true` for case sensitive or `false` for case insensitive sorting.
If `options` or its property `order` is unspecified, values are sorted in ascending sort order. Otherwise, specify an order of `'desc'` for descending or `'asc'` for ascending sort order of values.
#### Examples
Expand Down Expand Up @@ -413,15 +405,9 @@ import { compare } from 'natural-orderby';
// => ['$21.10', '$101.01', '$101.02', '$102.00']


// Default sort order case-sensitive

['A', 'b', 'C', 'd', 'E', 'f'].sort(compare());
// => ['A', 'C', 'E', 'b', 'd', 'f']


// Enabled case-insensitive sort order
// Case-insensitive sort order

['A', 'C', 'E', 'b', 'd', 'f'].sort(compare({ caseSensitive: false }));
['A', 'C', 'E', 'b', 'd', 'f'].sort(compare());
// => ['A', 'b', 'C', 'd', 'E', 'f']


Expand Down Expand Up @@ -545,6 +531,6 @@ Inspired by [The Alphanum Algorithm](http://www.davekoelle.com/alphanum.html) fr
## License
Licensed under the MIT License, Copyright © 2018 Olaf Ennen.
Licensed under the MIT License, Copyright © 2018 - present Olaf Ennen.
See [LICENSE](./LICENSE) for more information.
30 changes: 8 additions & 22 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,9 @@ orderBy(['$102.00', '$21.10', '$101.02', '$101.01']);
// => ['$21.10', '$101.01', '$101.02', '$102.00']


// Default sort order case-sensitive
// Case-insensitive sort order

orderBy(['A', 'b', 'C', 'd', 'E', 'f']);
// => ['A', 'C', 'E', 'b', 'd', 'f']


// Enabled case-insensitive sort order

orderBy(['A', 'C', 'E', 'b', 'd', 'f'], [v => v.toLowerCase()]);
orderBy(['A', 'C', 'E', 'b', 'd', 'f']);
// => ['A', 'b', 'C', 'd', 'E', 'f']


Expand Down Expand Up @@ -334,17 +328,15 @@ Creates a compare function that defines the natural sort order and which may be
compare(options?: CompareOptions): CompareFn
```
| Type | Value |
| :--------------- | :-------------------------------------------------------------------- |
| `CompareOptions` | <code>{ caseSensitive?: boolean, order?: 'asc' &#124; 'desc' }</code> |
| `CompareFn` | <code>(valueA: mixed, valueB: mixed) => number</code> |
| Type | Value |
| :--------------- | :---------------------------------------------------- |
| `CompareOptions` | <code>{ order?: 'asc' &#124; 'desc' }</code> |
| `CompareFn` | <code>(valueA: mixed, valueB: mixed) => number</code> |
#### Description
`compare()` returns a compare function that defines the natural sort order and which may be passed to [`Array.prototype.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort).
If `options` or its property `caseSensitive` is unspecified, values are sorted case sensitive. Otherwise, specify `true` for case sensitive or `false` for case insensitive sorting.
If `options` or its property `order` is unspecified, values are sorted in ascending sort order. Otherwise, specify an order of `'desc'` for descending or `'asc'` for ascending sort order of values.
#### Examples
Expand Down Expand Up @@ -404,15 +396,9 @@ import { compare } from 'natural-orderby';
// => ['$21.10', '$101.01', '$101.02', '$102.00']


// Default sort order case-sensitive

['A', 'b', 'C', 'd', 'E', 'f'].sort(compare());
// => ['A', 'C', 'E', 'b', 'd', 'f']


// Enabled case-insensitive sort order
// Case-insensitive sort order

['A', 'C', 'E', 'b', 'd', 'f'].sort(compare({ caseSensitive: false }));
['A', 'C', 'E', 'b', 'd', 'f'].sort(compare());
// => ['A', 'b', 'C', 'd', 'E', 'f']


Expand Down
31 changes: 1 addition & 30 deletions src/compare/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow
/* eslint-disable global-require */
const defaultOptions = {
caseSensitive: true,
order: 'asc',
};

Expand All @@ -22,21 +21,7 @@ describe('compare()', () => {
expect(baseCompare).toHaveBeenCalledTimes(1);
expect(baseCompare).toHaveBeenCalledWith(defaultOptions);
});
it('should call baseCompare() with { caseSensitive: false, order: "asc" } ', () => {
const compare = require('..').default;
const baseCompare = require('../../utils/baseCompare');
const options = {
caseSensitive: false,
};
const expectedOptions = {
...defaultOptions,
...options,
};
compare(options);
expect(baseCompare).toHaveBeenCalledTimes(1);
expect(baseCompare).toHaveBeenCalledWith(expectedOptions);
});
it('should call baseCompare() with { caseSensitive: true, order: "desc" } ', () => {
it('should call baseCompare() with { order: "desc" } ', () => {
const compare = require('..').default;
const baseCompare = require('../../utils/baseCompare');
const options = {
Expand All @@ -50,18 +35,4 @@ describe('compare()', () => {
expect(baseCompare).toHaveBeenCalledTimes(1);
expect(baseCompare).toHaveBeenCalledWith(expectedOptions);
});
it('should call baseCompare() with { caseSensitive: false, order: "desc" } ', () => {
const compare = require('..').default;
const baseCompare = require('../../utils/baseCompare');
const options = {
caseSensitive: false,
order: 'desc',
};
const expectedOptions = {
...options,
};
compare(options);
expect(baseCompare).toHaveBeenCalledTimes(1);
expect(baseCompare).toHaveBeenCalledWith(expectedOptions);
});
});
4 changes: 0 additions & 4 deletions src/compare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import type { CompareOptions, CompareFn } from '../types';
* Creates a compare function that defines the natural sort order considering
* the given `options` which may be passed to [`Array.prototype.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort).
*
* If `options` or its property `caseSensitive` is unspecified, values are
* sorted case sensitive. Otherwise, specify `true` for case sensitive or
* `false` for case insensitive sorting.
*
* If `options` or its property `order` is unspecified, values are sorted in
* ascending sort order. Otherwise, specify an order of `'desc'` for descending
* or `'asc'` for ascending sort order of values.
Expand Down
13 changes: 6 additions & 7 deletions src/types/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// @flow
export type CompareFn = (valueA: mixed, valueB: mixed) => number;

export type CaseSensitive = boolean;

export type OrderEnum = 'asc' | 'desc';

export type Order = OrderEnum | CompareFn;

export type CompareOptions = {|
caseSensitive?: CaseSensitive,
order?: OrderEnum,
|};
export type CompareOptions =
| {|
order?: OrderEnum,
|}
| OrderEnum
| void;

export type BaseCompareOptions = {|
caseSensitive: CaseSensitive,
order: Order,
|};

Expand Down

0 comments on commit 35c9d0d

Please sign in to comment.