[Pkg-javascript-commits] [node-music-library-index] 02/06: Imported Upstream version 1.3.0

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sat May 16 22:42:12 UTC 2015


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to branch master
in repository node-music-library-index.

commit 507d676f3c4086f07ed4ce260a0e5c16698470b7
Author: Andrew Kelley <superjoe30 at gmail.com>
Date:   Sat May 16 20:59:58 2015 +0000

    Imported Upstream version 1.3.0
---
 README.md    |  6 +++--
 index.js     | 62 ++++++++++++++++++++++++++++++++++++++++++++-----
 package.json | 12 +++++-----
 test/test.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 142 insertions(+), 14 deletions(-)

diff --git a/README.md b/README.md
index fbdb5d3..d0b8398 100644
--- a/README.md
+++ b/README.md
@@ -2,13 +2,13 @@
 
 Given track metadata objects, constructs a searchable object model.
 This module is used both in the client and the server of
-[Groove Basin](https://github.com/superjoe30/groovebasin).
+[Groove Basin](https://github.com/andrewrk/groovebasin).
 
 ## Features
 
  * Sort order ignores 'a', 'an' and 'the' in artists, albums, and names.
  * Sorting and searching is case insensitive and
-   [diacritics-insensitive](https://github.com/superjoe30/diacritics).
+   [diacritics-insensitive](https://github.com/andrewrk/diacritics).
  * Searching uses word-based filtering (this is how most music player
    applications implement filtering) on all track fields.
  * Distinguishes albums by name, date, and album artist.
@@ -21,6 +21,8 @@ This module is used both in the client and the server of
    * Tracks by user-defined key.
    * Artists by library-defined key.
    * Albums by library-defined key.
+ * Searching allows the use of quotes (`"`) to match multiple word searches
+   and backslash (`\`) to escape literal quotes and backslashes.
 
 ## Usage
 
diff --git a/index.js b/index.js
index d62b9af..3e55385 100644
--- a/index.js
+++ b/index.js
@@ -8,6 +8,7 @@ MusicLibraryIndex.defaultVariousArtistsKey = "VariousArtists";
 MusicLibraryIndex.defaultVariousArtistsName = "Various Artists";
 MusicLibraryIndex.defaultSearchFields = ['artistName', 'albumArtistName',
   'albumName', 'name'];
+MusicLibraryIndex.parseQueryIntoTerms = parseQueryIntoTerms;
 
 function MusicLibraryIndex(options) {
   options = options || {};
@@ -249,8 +250,6 @@ MusicLibraryIndex.prototype.removeTrack = function(key) {
 }
 
 MusicLibraryIndex.prototype.search = function(query) {
-  query = query.trim();
-
   var searchResults = new MusicLibraryIndex({
     searchFields: this.searchFields,
     variousArtistsKey: this.variousArtistsKey,
@@ -258,7 +257,7 @@ MusicLibraryIndex.prototype.search = function(query) {
     prefixesToStrip: this.prefixesToStrip,
   });
 
-  var words = formatSearchable(query).split(/\s+/);
+  var terms = parseQueryIntoTerms(query);
 
   var track;
   for (var trackKey in this.trackTable) {
@@ -274,9 +273,9 @@ MusicLibraryIndex.prototype.search = function(query) {
   return searchResults;
 
   function testMatch() {
-    for (var i = 0; i < words.length; i += 1) {
-      var word = words[i];
-      if (track.searchTags.indexOf(word) === -1) {
+    for (var i = 0; i < terms.length; i += 1) {
+      var term = terms[i];
+      if (track.searchTags.indexOf(term) === -1) {
         return false;
       }
     }
@@ -284,6 +283,57 @@ MusicLibraryIndex.prototype.search = function(query) {
   }
 };
 
+function parseQueryIntoTerms(query) {
+  var normalizedQuery = formatSearchable(query.trim());
+  var term = "";
+  var inQuote = false;
+  var inEscape = false;
+  var termBoundary = true;
+  var terms = [];
+
+  for (var i = 0; i < normalizedQuery.length; i += 1) {
+    var c = normalizedQuery[i];
+    if (inEscape) {
+      term += c;
+      termBoundary = false;
+      inEscape = false;
+    } else if (/\s/.test(c) && !inQuote) {
+      flushTerm();
+      termBoundary = true;
+    } else if (c === '\\') {
+      inEscape = true;
+    } else if (c === '"') {
+      if (inQuote) {
+        inQuote = false;
+        flushTerm();
+      } else if (termBoundary) {
+        inQuote = true;
+      } else {
+        term += c;
+      }
+    } else {
+      term += c;
+      termBoundary = false;
+    }
+  }
+  flushTerm();
+
+  return terms;
+
+  function flushTerm() {
+    if (inQuote) {
+      term = "\"" + term;
+    }
+    if (inEscape) {
+      term += "\\";
+    }
+    if (term) {
+      terms.push(term);
+      term = "";
+    }
+  }
+}
+
 function getOrCreate(key, table, initObjFunc) {
   var result = table[key];
   if (result == null) {
diff --git a/package.json b/package.json
index b10bc22..d80ce3d 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "music-library-index",
-  "version": "1.2.2",
+  "version": "1.3.0",
   "description": "build a searchable javascript object model given track metadata objects",
   "main": "index.js",
   "scripts": {
@@ -9,19 +9,19 @@
   "author": "Andrew Kelley <superjoe30 at gmail.com>",
   "license": "MIT",
   "dependencies": {
-    "diacritics": "~1.2.0"
+    "diacritics": "~1.2.1"
   },
   "devDependencies": {
-    "mocha": "~1.21.0"
+    "mocha": "~2.0.1"
   },
   "repository": {
     "type": "git",
-    "url": "git://github.com/superjoe30/node-music-library-index.git"
+    "url": "git://github.com/andrewrk/node-music-library-index.git"
   },
   "bugs": {
-    "url": "https://github.com/superjoe30/node-music-library-index/issues"
+    "url": "https://github.com/andrewrk/node-music-library-index/issues"
   },
-  "homepage": "https://github.com/superjoe30/node-music-library-index",
+  "homepage": "https://github.com/andrewrk/node-music-library-index",
   "directories": {
     "test": "test"
   }
diff --git a/test/test.js b/test/test.js
index 8ae2ab8..d041720 100644
--- a/test/test.js
+++ b/test/test.js
@@ -446,3 +446,79 @@ describe("album with album artist", function() {
     assert.strictEqual(library.artistList.length, 1);
   });
 });
+
+describe("parseQueryIntoTerms", function() {
+  it("works", function() {
+    assert.deepEqual(MusicLibraryIndex.parseQueryIntoTerms(""), []);
+    assert.deepEqual(MusicLibraryIndex.parseQueryIntoTerms("a"), ["a"]);
+    assert.deepEqual(MusicLibraryIndex.parseQueryIntoTerms(" a   b "), ["a", "b"]);
+    assert.deepEqual(MusicLibraryIndex.parseQueryIntoTerms("\"a  b\""), ["a  b"]);
+    assert.deepEqual(MusicLibraryIndex.parseQueryIntoTerms("\"a  b\\\" c\""), ["a  b\" c"]);
+    assert.deepEqual(MusicLibraryIndex.parseQueryIntoTerms("\\\"a  b\""), ["\"a", "b\""]);
+    assert.deepEqual(MusicLibraryIndex.parseQueryIntoTerms("\""), ["\""]);
+    assert.deepEqual(MusicLibraryIndex.parseQueryIntoTerms("\\"), ["\\"]);
+    assert.deepEqual(MusicLibraryIndex.parseQueryIntoTerms("\"\""), []);
+    assert.deepEqual(MusicLibraryIndex.parseQueryIntoTerms("a\" b\"c"), ["a\"", "b\"c"]);
+    assert.deepEqual(MusicLibraryIndex.parseQueryIntoTerms("ab cd e"), ["ab", "cd", "e"]);
+  });
+});
+
+describe("searching with quoted seach terms", function() {
+  var library = new MusicLibraryIndex();
+
+  library.addTrack({
+    key: "fUPmxjMc",
+    name: "Été (Original Mix)",
+    artistName: "AKA AKA & Thalstroem",
+    albumName: "Varieté",
+  });
+
+  library.addTrack({
+    key: "zyGaKkrU",
+    name: "Tribute to Young Stroke AKA Young Muscle",
+    artistName: "Andy Kelley",
+    albumName: "The Weekend Challenge #3",
+  });
+
+  var literalQuoteKey = "G5FqXeJZ";
+  library.addTrack({
+    key: literalQuoteKey,
+    name: "A song with a literal \" in it",
+    artistName: "Tester",
+    albumName: "literalQuote",
+  });
+
+  var literalBackslashKey = "Xsc4+ril";
+  library.addTrack({
+    key: literalBackslashKey,
+    name: "A song with a literal \\ in it",
+    artistName: "Tester",
+    albumName: "literalBackslash",
+  });
+
+  library.rebuild();
+
+  it("single search term returns both", function() {
+    var results = library.search("aka aka");
+    assert.strictEqual(results.artistList.length, 2);
+  });
+
+  it("quoted search term", function() {
+    var results = library.search("\"aka aka\"");
+    assert.strictEqual(results.artistList.length, 1);
+    assert.strictEqual(results.artistList[0].name, "AKA AKA & Thalstroem");
+  });
+
+  it("matches a song with a literal quote", function() {
+    var results = library.search("\"");
+    assert.strictEqual(results.albumList.length, 1);
+    assert.strictEqual(results.albumList[0].trackList.length, 1);
+    assert.strictEqual(results.albumList[0].trackList[0].key, literalQuoteKey);
+  });
+  it("matches a song with a literal backslash", function() {
+    var results = library.search("\\");
+    assert.strictEqual(results.albumList.length, 1);
+    assert.strictEqual(results.albumList[0].trackList.length, 1);
+    assert.strictEqual(results.albumList[0].trackList[0].key, literalBackslashKey);
+  });
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-music-library-index.git



More information about the Pkg-javascript-commits mailing list