Permalink
Browse files

add test of natSort

  • Loading branch information...
minodisk committed Apr 11, 2012
1 parent bb2a76c commit 716cb362f13f256d9bdbb9d53cae38aa1bc2107f
Showing with 448 additions and 443 deletions.
  1. +43 −4 README.md
  2. +16 −21 lib/browser/sorter.js
  3. +1 −1 lib/browser/sorter.min.js
  4. +16 −21 lib/node/sorter.js
  5. +14 −5 package.json
  6. +12 −27 src/sorter.coffee
  7. +346 −0 test/test-sorter.coffee
  8. +0 −364 test/test.coffee
View
@@ -1,13 +1,12 @@
# Sorter
Dictionary and natural sort module for Node.js, RequireJS and browser.
-Supports 2 bytes character in English and Japanese.
## Installation at Node.js
$ npm install sorter
-## Adding to browser
+## Adding to browser source
### RequireJS
@@ -29,5 +28,45 @@ Supports 2 bytes character in English and Japanese.
## API Documentation
-* **dictSort(array, key = null)** -
-* **natSort(array, key = null)** -
+* **dictSort(array\[, key\])** - Returns natural ordered array.
+* **natSort(array\[, key\])** - Returns dictionary ordered array.
+
+## Dictionary Sort
+
+Array#sort()
+
+ AD
+ AM
+ BC
+ PM
+ after
+ before
+
+sorter.dictSort()
+
+ AD
+ after
+ AM
+ BC
+ before
+ PM
+
+## Natural Sort
+
+Array#sort()
+
+ image_005.jpg
+ image_04.jpg
+ image_1.jpg
+ image_12.jpg
+ image_21.jpg
+ image_4.jpg
+
+sorter.natSort()
+
+ image_1.jpg
+ image_04.jpg
+ image_4.jpg
+ image_005.jpg
+ image_12.jpg
+ image_21.jpg
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,13 +1,22 @@
{
"name" :"sorter",
- "version" :"0.0.0",
+ "version" :"0.0.1",
"description":"Dictionary and natural sort module.",
"tags" :["dictionary natural sort array"],
- "author" :"Daisuke MINO <daisuke.mino@gmail.com>",
- "license" :"MIT",
- "main" :"./lib/node/sorter",
+ "author" :{
+ "name" :"Daisuke MINO",
+ "email":"daisuke.mino@gmail.com"
+ },
"repository" :{
"type":"git",
"url" :"https://minodisk@github.com/minodisk/sorter-js.git"
- }
+ },
+ "engines":[
+ "node >= 0.6.0"
+ ],
+ "license" :"MIT",
+ "dependencies":{
+ "underscore": ">=1.3.2"
+ },
+ "main" :"./lib/node/sorter"
}
View
@@ -178,45 +178,30 @@ toDictCode = (text, index)->
exports.natSort = (src, key = null)->
tmps = []
- console.log src
for v, i in src
v = if key then v[key] else v
+
+ chunks = []
+ v.replace R_NUM_SPLITTER, (matched, num, str)->
+ chunk = {}
+ if num
+ num = num.replace R_MULTIBYTE_NUM, (matched)->
+ '0123456789'.indexOf matched
+ chunk.num = Number num
+ chunk.str = matched
+ chunks.push chunk
+
tmps[i] =
raw : v
- chunks: natParse v
+ chunks: chunks
tmps.sort(natCompare)
dst = []
i = tmps.length
while i--
dst[i] = tmps[i].raw
dst
-natParse = (text)->
- chunks = []
- text.replace R_NUM_SPLITTER, (matched, num, str)->
- chunk = {}
- if num
- chunk.num = Number(num.replace R_MULTIBYTE_NUM, (matched)->
- '0123456789'.indexOf(matched)
- )
- chunk.str = matched
- chunks.push(chunk)
- #console.log(text, '->', JSON.stringify(chunks))
- chunks
-
natCompare = (a, b)->
- # if (typeof a.num isnt 'undefined' and typeof b.num isnt 'undefined') {
- # if ((d = a.num - b.num) isnt 0) {
- # return d
- # } else if ((d = a.raw.length - b.raw.length) isnt 0) {
- # return d
- # }
- # return dictionaryCompare(a.raw, b.raw)
- # } else if (typeof a.num isnt 'undefined') {
- # return -1
- # } else if (typeof b.num isnt 'undefined') {
- # return 1
- # }
a = a.chunks
b = b.chunks
Oops, something went wrong.

0 comments on commit 716cb36

Please sign in to comment.