web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.2 KiB

  1. # String Natural Compare
  2. [![NPM Version](https://img.shields.io/npm/v/string-natural-compare.svg)](https://www.npmjs.com/package/string-natural-compare)
  3. [![Build Status](https://travis-ci.org/nwoltman/string-natural-compare.svg?branch=master)](https://travis-ci.org/nwoltman/string-natural-compare)
  4. [![Coverage Status](https://coveralls.io/repos/nwoltman/string-natural-compare/badge.svg?branch=master)](https://coveralls.io/r/nwoltman/string-natural-compare?branch=master)
  5. [![Dependencies Status](https://img.shields.io/david/nwoltman/string-natural-compare)](https://david-dm.org/nwoltman/string-natural-compare)
  6. Compare alphanumeric strings the same way a human would, using a natural order algorithm (originally known as the [alphanum algorithm](http://davekoelle.com/alphanum.html)) where numeric characters are sorted based on their numeric values rather than their ASCII values.
  7. ```
  8. Standard sorting: Natural order sorting:
  9. img1.png img1.png
  10. img10.png img2.png
  11. img12.png img10.png
  12. img2.png img12.png
  13. ```
  14. This module exports a function that returns a number indicating whether one string should come before, after, or is the same as another string.
  15. It can be used directly with the native [`.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) array method.
  16. ### Fast and Robust
  17. This module can compare strings containing any size of number and is heavily tested with a custom [benchmark suite](https://github.com/nwoltman/string-natural-compare/tree/master/benchmark) to make sure that it is as fast as possible.
  18. ## Installation
  19. ```sh
  20. npm install string-natural-compare --save
  21. # or
  22. yarn add string-natural-compare
  23. ```
  24. ## Usage
  25. #### `naturalCompare(strA, strB[, options])`
  26. + `strA` (_string_)
  27. + `strB` (_string_)
  28. + `options` (_object_) - Optional options object with the following options:
  29. + `caseInsensitive` (_boolean_) - Set to `true` to compare strings case-insensitively. Default: `false`.
  30. + `alphabet` (_string_) - A string of characters that define a custom character ordering. Default: `undefined`.
  31. ```js
  32. const naturalCompare = require('string-natural-compare');
  33. // Simple, case-sensitive sorting
  34. const files = ['z1.doc', 'z10.doc', 'z17.doc', 'z2.doc', 'z23.doc', 'z3.doc'];
  35. files.sort(naturalCompare);
  36. // -> ['z1.doc', 'z2.doc', 'z3.doc', 'z10.doc', 'z17.doc', 'z23.doc']
  37. // Case-insensitive sorting
  38. const chars = ['B', 'C', 'a', 'd'];
  39. const naturalCompareCI = (a, b) => naturalCompare(a, b, {caseInsensitive: true});
  40. chars.sort(naturalCompareCI);
  41. // -> ['a', 'B', 'C', 'd']
  42. // Note:
  43. ['a', 'A'].sort(naturalCompareCI); // -> ['a', 'A']
  44. ['A', 'a'].sort(naturalCompareCI); // -> ['A', 'a']
  45. // Compare strings containing large numbers
  46. naturalCompare(
  47. '1165874568735487968325787328996865',
  48. '265812277985321589735871687040841'
  49. );
  50. // -> 1
  51. // (Other inputs with the same ordering as this example may yield a different number > 0)
  52. // Sorting an array of objects
  53. const hotelRooms = [
  54. {street: '350 5th Ave', room: 'A-1021'},
  55. {street: '350 5th Ave', room: 'A-21046-b'}
  56. ];
  57. // Sort by street (case-insensitive), then by room (case-sensitive)
  58. hotelRooms.sort((a, b) => (
  59. naturalCompare(a.street, b.street, {caseInsensitive: true}) ||
  60. naturalCompare(a.room, b.room)
  61. ));
  62. // When text transformation is needed or when doing a case-insensitive sort on a
  63. // large array of objects, it is best for performance to pre-compute the
  64. // transformed text and store it on the object. This way, the text will not need
  65. // to be transformed for every comparison while sorting.
  66. const cars = [
  67. {make: 'Audi', model: 'R8'},
  68. {make: 'Porsche', model: '911 Turbo S'}
  69. ];
  70. // Sort by make, then by model (both case-insensitive)
  71. for (const car of cars) {
  72. car.sortKey = (car.make + ' ' + car.model).toLowerCase();
  73. }
  74. cars.sort((a, b) => naturalCompare(a.sortKey, b.sortKey));
  75. // Using a custom alphabet (Russian alphabet)
  76. const russianOpts = {
  77. alphabet: 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя',
  78. };
  79. ['Ё', 'А', 'б', 'Б'].sort((a, b) => naturalCompare(a, b, russianOpts));
  80. // -> ['А', 'Б', 'Ё', 'б']
  81. ```
  82. **Note:** Putting numbers in the custom alphabet can cause undefined behaviour.