-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from amiel/feature/support-nested-query-params
Support nested query params
- Loading branch information
Showing
13 changed files
with
196 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// This was inspired by https://stackoverflow.com/a/34514143/223519 | ||
// and ultimately reconfigured by @basz | ||
// https://github.com/amiel/ember-data-url-templates/issues/17#issuecomment-379232452 | ||
|
||
export default function flattenQueryParams(arr) { | ||
let newObj = {}; | ||
dive('', arr, newObj); | ||
return newObj; | ||
} | ||
|
||
function dive(currentKey, into, target) { | ||
for (let i in into) { | ||
if (into.hasOwnProperty(i)) { | ||
let newKey = i; | ||
let newVal = into[i]; | ||
|
||
if (currentKey.length > 0) { | ||
newKey = `${currentKey}[${i}]`; | ||
} | ||
|
||
if (typeof newVal === 'object') { | ||
dive(newKey, newVal, target); | ||
} else { | ||
target[newKey] = newVal; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-data-url-templates/utils/flatten-query-params'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Ember from 'ember'; | ||
|
||
export default Ember.Route.extend({ | ||
model({ term }) { | ||
return this.store.query('post', { filter: { term } }); | ||
}, | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>Search Results</h1> | ||
|
||
<div id="posts"> | ||
{{#each model as |post|}} | ||
<div id="post-{{post.id}}" class="post"> | ||
{{#link-to 'post' post.slug}}{{post.title}}{{/link-to}} | ||
</div> | ||
{{/each}} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* NOTE: This was taken from https://github.com/canjs/can-deparam/blob/master/can-deparam.js */ | ||
|
||
/** | ||
* @module {function} can-deparam can-deparam | ||
* @parent can-routing | ||
* @collection can-infrastructure | ||
* @description Deserialize a query string into an array or object. | ||
* @signature `deparam(params)` | ||
* | ||
* @param {String} params a form-urlencoded string of key-value pairs | ||
* @return {Object} The params formatted into an object | ||
* | ||
* Takes a string of name value pairs and returns a Object literal that represents those params. | ||
* | ||
* ```js | ||
* console.log(JSON.stringify(deparam("?foo=bar&number=1234"))); // -> '{"foo" : "bar", "number": 1234}' | ||
* console.log(JSON.stringify(deparam("#foo[]=bar&foo[]=baz"))); // -> '{"foo" : ["bar", "baz"]}' | ||
* console.log(JSON.stringify(deparam("foo=bar%20%26%20baz"))); // -> '{"foo" : "bar & baz"}' | ||
* ``` | ||
* @body | ||
* | ||
* ## Try it | ||
* | ||
* Use this JS Bin to play around with this package: | ||
* | ||
* <a class="jsbin-embed" href="https://jsbin.com/mobimok/3/embed?js,console">can-deparam on jsbin.com</a> | ||
* <script src="https://static.jsbin.com/js/embed.min.js?4.0.4"></script> | ||
*/ | ||
var digitTest = /^\d+$/, | ||
keyBreaker = /([^\[\]]+)|(\[\])/g, | ||
paramTest = /([^?#]*)(#.*)?$/, | ||
entityRegex = /%([^0-9a-f][0-9a-f]|[0-9a-f][^0-9a-f]|[^0-9a-f][^0-9a-f])/i, | ||
prep = function (str) { | ||
str = str.replace(/\+/g, ' '); | ||
|
||
try { | ||
return decodeURIComponent(str); | ||
} | ||
catch (e) { | ||
return decodeURIComponent(str.replace(entityRegex, function(match, hex) { | ||
return '%25' + hex; | ||
})); | ||
} | ||
}; | ||
|
||
export default function(params) { | ||
var data = {}, pairs, lastPart; | ||
if (params && paramTest.test(params)) { | ||
pairs = params.split('&'); | ||
pairs.forEach(function (pair) { | ||
var parts = pair.split('='), | ||
key = prep(parts.shift()), | ||
value = prep(parts.join('=')), | ||
current = data; | ||
if (key) { | ||
parts = key.match(keyBreaker); | ||
for (var j = 0, l = parts.length - 1; j < l; j++) { | ||
if (!current[parts[j]]) { | ||
// If what we are pointing to looks like an `array` | ||
current[parts[j]] = digitTest.test(parts[j + 1]) || parts[j + 1] === '[]' ? [] : {}; | ||
} | ||
current = current[parts[j]]; | ||
} | ||
lastPart = parts.pop(); | ||
if (lastPart === '[]') { | ||
current.push(value); | ||
} else { | ||
current[lastPart] = value; | ||
} | ||
} | ||
}); | ||
} | ||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import flattenQueryParams from 'dummy/utils/flatten-query-params'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Unit | Utility | flatten query params'); | ||
|
||
test('does not alter a simple object', function(assert) { | ||
const object = { foo: 'bar' }; | ||
const result = flattenQueryParams(object); | ||
assert.deepEqual(object, result); | ||
}); | ||
|
||
test('can flatten a simple nested query', function(assert) { | ||
const object = { filter: { foo: 'bar' } }; | ||
const result = flattenQueryParams(object); | ||
assert.deepEqual(result, { 'filter[foo]': 'bar' }); | ||
}); |