« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--package.json1
-rwxr-xr-xsrc/crawl-local.js7
-rw-r--r--todo.txt4
-rw-r--r--yarn.lock4
4 files changed, 11 insertions, 5 deletions
diff --git a/package.json b/package.json
index 4ed3f41..dba3bd8 100644
--- a/package.json
+++ b/package.json
@@ -16,6 +16,7 @@
   "dependencies": {
     "cheerio": "^1.0.0-rc.1",
     "node-fetch": "^1.7.0",
+    "node-natural-sort": "^0.8.6",
     "sanitize-filename": "^1.6.1",
     "tempy": "^0.1.0"
   }
diff --git a/src/crawl-local.js b/src/crawl-local.js
index cd213e0..4c510a1 100755
--- a/src/crawl-local.js
+++ b/src/crawl-local.js
@@ -4,6 +4,7 @@
 
 const fs = require('fs')
 const path = require('path')
+const naturalSort = require('node-natural-sort')
 
 const { promisify } = require('util')
 const readDir = promisify(fs.readdir)
@@ -11,11 +12,7 @@ const stat = promisify(fs.stat)
 
 function crawl(dirPath) {
   return readDir(dirPath).then(items => {
-    items.sort((a, b) => {
-      const aUp = a.toUpperCase()
-      const bUp = b.toUpperCase()
-      return (aUp < bUp) ? -1 : (aUp == bUp) ? 0 : 1
-    })
+    items.sort(naturalSort())
 
     return Promise.all(items.map(item => {
       const itemPath = path.join(dirPath, item)
diff --git a/todo.txt b/todo.txt
index 29bdda0..032df54 100644
--- a/todo.txt
+++ b/todo.txt
@@ -80,3 +80,7 @@ TODO: Recursive local file playlist crawler.
 TODO: *Requiring* a literal `playlist.json` file doesn't seem quite right,
       especially since there's the `--open` option.
       (Done!)
+
+TODO: Make local downlaoder sort more natural - that is, 10 comes immediately
+      after 9, not 1. (1, 5, 9, 10, 12; not 1, 10, 12, 5, 9.)
+      (Done!)
diff --git a/yarn.lock b/yarn.lock
index ae001ba..d459bde 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -120,6 +120,10 @@ node-fetch@^1.7.0:
     encoding "^0.1.11"
     is-stream "^1.0.1"
 
+node-natural-sort:
+  version "0.8.6"
+  resolved "https://registry.yarnpkg.com/node-natural-sort/-/node-natural-sort-0.8.6.tgz#01dc6badc4743b16033408f0d8589ab2e6c094cf"
+
 nth-check@~1.0.1:
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/nth-check/-/nth-check-1.0.1.tgz#9929acdf628fc2c41098deab82ac580cf149aae4"