Skip to content

Commit

Permalink
chore(all): prepare release 1.0.0-beta.1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
EisenbergEffect committed Mar 1, 2016
1 parent 94fce2b commit 88dcaba
Show file tree
Hide file tree
Showing 14 changed files with 433 additions and 202 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aurelia-path",
"version": "1.0.0-beta.1.1.0",
"version": "1.0.0-beta.1.1.1",
"description": "Utilities for path manipulation.",
"keywords": [
"aurelia",
Expand Down
2 changes: 1 addition & 1 deletion dist/amd/aurelia-path.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ declare module 'aurelia-path' {
/**
* Parse a query string.
*
* @param The query string to parse.
* @param queryString The query string to parse.
* @returns Object with keys and values mapped from the query string.
*/
export function parseQueryString(queryString: string): Object;
Expand Down
110 changes: 72 additions & 38 deletions dist/amd/aurelia-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,37 @@ define(['exports'], function (exports) {
return scheme + urlPrefix + url3.join('/') + trailingSlash;
}

var encode = encodeURIComponent;
var encodeKey = function encodeKey(k) {
return encode(k).replace('%24', '$');
};

function buildParam(key, value) {
var result = [];
if (value === null || value === undefined) {
return result;
}
if (Array.isArray(value)) {
for (var i = 0, l = value.length; i < l; i++) {
var arrayKey = key + '[' + (typeof value[i] === 'object' && value[i] !== null ? i : '') + ']';
result = result.concat(buildParam(arrayKey, value[i]));
}
} else if (typeof value === 'object') {
for (var propertyName in value) {
result = result.concat(buildParam(key + '[' + propertyName + ']', value[propertyName]));
}
} else {
result.push(encodeKey(key) + '=' + encode(value));
}
return result;
}

function buildQueryString(params) {
var pairs = [];
var keys = Object.keys(params || {}).sort();
var encode = encodeURIComponent;
var encodeKey = function encodeKey(k) {
return encode(k).replace('%24', '$');
};

for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i];
var value = params[key];
if (value === null || value === undefined) {
continue;
}

if (Array.isArray(value)) {
var arrayKey = encodeKey(key) + '[]';
for (var j = 0, l = value.length; j < l; j++) {
pairs.push(arrayKey + '=' + encode(value[j]));
}
} else {
pairs.push(encodeKey(key) + '=' + encode(value));
}
pairs = pairs.concat(buildParam(key, params[key]));
}

if (pairs.length === 0) {
Expand All @@ -122,6 +130,31 @@ define(['exports'], function (exports) {
return pairs.join('&');
}

function processScalarParam(existedParam, value, isPrimitive) {
if (Array.isArray(existedParam)) {
existedParam.push(value);
return existedParam;
}
if (existedParam !== undefined) {
return isPrimitive ? value : [existedParam, value];
}

return value;
}

function parseComplexParam(queryParams, keys, value) {
var currentParams = queryParams;
var keysLastIndex = keys.length - 1;
for (var j = 0; j <= keysLastIndex; j++) {
var key = keys[j] === '' ? currentParams.length : keys[j];
if (j < keysLastIndex) {
currentParams = currentParams[key] = currentParams[key] || (keys[j + 1] ? {} : []);
} else {
currentParams = currentParams[key] = value;
}
}
}

function parseQueryString(queryString) {
var queryParams = {};
if (!queryString || typeof queryString !== 'string') {
Expand All @@ -133,37 +166,38 @@ define(['exports'], function (exports) {
query = query.substr(1);
}

var pairs = query.split('&');
var pairs = query.replace(/\+/g, ' ').split('&');
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split('=');
var key = decodeURIComponent(pair[0]);
var keyLength = key.length;
var isArray = false;
var value = undefined;

var isPrimitive = false;
if (!key) {
continue;
} else if (pair.length === 1) {
value = true;
} else {
if (keyLength > 2 && key.slice(keyLength - 2) === '[]') {
isArray = true;
key = key.slice(0, keyLength - 2);
if (!queryParams[key]) {
queryParams[key] = [];
}
}
}

value = pair[1] ? decodeURIComponent(pair[1]) : '';
var keys = key.split('][');
var keysLastIndex = keys.length - 1;

if (/\[/.test(keys[0]) && /\]$/.test(keys[keysLastIndex])) {
keys[keysLastIndex] = keys[keysLastIndex].replace(/\]$/, '');
keys = keys.shift().split('[').concat(keys);
keysLastIndex = keys.length - 1;
} else {
isPrimitive = true;
keysLastIndex = 0;
}

if (isArray) {
queryParams[key].push(value);
if (pair.length === 2) {
var value = pair[1] ? decodeURIComponent(pair[1]) : '';
if (keysLastIndex) {
parseComplexParam(queryParams, keys, value);
} else {
queryParams[key] = processScalarParam(queryParams[key], value, isPrimitive);
}
} else {
queryParams[key] = value;
queryParams[key] = true;
}
}

return queryParams;
}
});
2 changes: 1 addition & 1 deletion dist/aurelia-path.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ declare module 'aurelia-path' {
/**
* Parse a query string.
*
* @param The query string to parse.
* @param queryString The query string to parse.
* @returns Object with keys and values mapped from the query string.
*/
export function parseQueryString(queryString: string): Object;
Expand Down
140 changes: 100 additions & 40 deletions dist/aurelia-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ function trimDots(ary: string[]): void {
i -= 2;
}
}
}
}
}}

/**
* Calcualtes a path relative to a file.
Expand Down Expand Up @@ -106,6 +105,34 @@ export function join(path1: string, path2: string): string {
return scheme + urlPrefix + url3.join('/') + trailingSlash;
}

let encode = encodeURIComponent;
let encodeKey = k => encode(k).replace('%24', '$');
/**
* Recursively builds part of query string for parameter.
*
* @param key Parameter name for query string.
* @param value Parameter value to deserialize.
* @return Array with serialized parameter(s)
*/
function buildParam(key: string, value: any): Array<string> {
let result = [];
if (value === null || value === undefined) {
return result;
}
if (Array.isArray(value)) {
for (let i = 0, l = value.length; i < l; i++) {
let arrayKey = key + '[' + (typeof value[i] === 'object' && value[i] !== null ? i : '') + ']';
result = result.concat(buildParam(arrayKey, value[i]));
}
} else if (typeof (value) === 'object') {
for (let propertyName in value) {
result = result.concat(buildParam(key + '[' + propertyName + ']', value[propertyName]));
}
} else {
result.push(`${encodeKey(key) }=${encode(value) }`);
}
return result;
}

/**
* Generate a query string from an object.
Expand All @@ -116,24 +143,9 @@ export function join(path1: string, path2: string): string {
export function buildQueryString(params: Object): string {
let pairs = [];
let keys = Object.keys(params || {}).sort();
let encode = encodeURIComponent;
let encodeKey = k => encode(k).replace('%24', '$');

for (let i = 0, len = keys.length; i < len; i++) {
let key = keys[i];
let value = params[key];
if (value === null || value === undefined) {
continue;
}

if (Array.isArray(value)) {
let arrayKey = `${encodeKey(key)}[]`;
for (let j = 0, l = value.length; j < l; j++) {
pairs.push(`${arrayKey}=${encode(value[j])}`);
}
} else {
pairs.push(`${encodeKey(key)}=${encode(value)}`);
}
pairs = pairs.concat(buildParam(key, params[key]));
}

if (pairs.length === 0) {
Expand All @@ -143,10 +155,55 @@ export function buildQueryString(params: Object): string {
return pairs.join('&');
}

/**
* Process parameter that was recognized as scalar param (primitive value or shallow array).
*
* @param existedParam Object with previously parsed values for specified key.
* @param value Parameter value to append.
* @param isPrimitive If true and parameter value already specified - method overwrites it. If false - transofrm parameter to array and append new value to it.
* @returns Initial primitive value or transformed existedParam if parameter was recognized as an array.
*/
function processScalarParam(existedParam: Object, value: Object, isPrimitive: boolean): Object {
if (Array.isArray(existedParam)) {
// value is already an array, so push on the next value.
existedParam.push(value);
return existedParam;
}
if (existedParam !== undefined) {
// value isn't an array, but since a second value has been specified,
// convert value into an array.
return isPrimitive ? value : [existedParam, value];
}
// value is a scalar.
return value;
}
/**
* Sequentially process parameter that was recognized as complex value (object or array).
* For each keys part, if the current level is undefined create an
* object or array based on the type of the next keys part.
*
* @param queryParams root-level result object.
* @param keys Collection of keys related to this parameter.
* @param value Parameter value to append.
*/
function parseComplexParam(queryParams: Object, keys: Object, value: any): void {
let currentParams = queryParams;
let keysLastIndex = keys.length - 1;
for (let j = 0; j <= keysLastIndex; j++) {
let key = keys[j] === '' ? currentParams.length : keys[j];
if (j < keysLastIndex) {
currentParams = currentParams[key] = currentParams[key] || (keys[j + 1] ? {} : []);
} else {
currentParams = currentParams[key] = value;
}
}
}


/**
* Parse a query string.
*
* @param The query string to parse.
* @param queryString The query string to parse.
* @returns Object with keys and values mapped from the query string.
*/
export function parseQueryString(queryString: string): Object {
Expand All @@ -160,37 +217,40 @@ export function parseQueryString(queryString: string): Object {
query = query.substr(1);
}

let pairs = query.split('&');
let pairs = query.replace(/\+/g, ' ').split('&');
for (let i = 0; i < pairs.length; i++) {
let pair = pairs[i].split('=');
let key = decodeURIComponent(pair[0]);
let keyLength = key.length;
let isArray = false;
let value;

let isPrimitive = false;
if (!key) {
continue;
} else if (pair.length === 1) {
value = true;
} else {
//Handle arrays
if (keyLength > 2 && key.slice(keyLength - 2) === '[]') {
isArray = true;
key = key.slice(0, keyLength - 2);
if (!queryParams[key]) {
queryParams[key] = [];
}
}
}
//split object key into its parts
let keys = key.split('][');
let keysLastIndex = keys.length - 1;

value = pair[1] ? decodeURIComponent(pair[1]) : '';
// If the first keys part contains [ and the last ends with ], then []
// are correctly balanced, split key to parts
//Else it's basic key
if (/\[/.test(keys[0]) && /\]$/.test(keys[keysLastIndex])) {
keys[keysLastIndex] = keys[keysLastIndex].replace(/\]$/, '');
keys = keys.shift().split('[').concat(keys);
keysLastIndex = keys.length - 1;
} else {
isPrimitive = true;
keysLastIndex = 0;
}

if (isArray) {
queryParams[key].push(value);
if (pair.length === 2) {
let value = pair[1] ? decodeURIComponent(pair[1]) : '';
if (keysLastIndex) {
parseComplexParam(queryParams, keys, value);
} else {
queryParams[key] = processScalarParam(queryParams[key], value, isPrimitive);
}
} else {
queryParams[key] = value;
queryParams[key] = true;
}
}

return queryParams;
}
2 changes: 1 addition & 1 deletion dist/commonjs/aurelia-path.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ declare module 'aurelia-path' {
/**
* Parse a query string.
*
* @param The query string to parse.
* @param queryString The query string to parse.
* @returns Object with keys and values mapped from the query string.
*/
export function parseQueryString(queryString: string): Object;
Expand Down
Loading

0 comments on commit 88dcaba

Please sign in to comment.