From addfe72506043d814f77f6a02c392c79b225a3a6 Mon Sep 17 00:00:00 2001 From: worka Date: Thu, 5 Mar 2020 20:13:30 +1000 Subject: [PATCH] default export changed to named export --- dist/url.js | 43 +++++++++++++++++++++++++------------------ dist/url.min.js | 2 +- src/url.js | 22 +++++++++++++++++----- src/url.test.js | 44 ++++++++++++++++++++++---------------------- 4 files changed, 65 insertions(+), 46 deletions(-) diff --git a/dist/url.js b/dist/url.js index 3c8c783..4fe2cf9 100644 --- a/dist/url.js +++ b/dist/url.js @@ -1,10 +1,10 @@ (function(global, factory) { typeof exports === 'object' && typeof module !== 'undefined' - ? (module.exports = factory()) + ? factory(exports) : typeof define === 'function' && define.amd - ? define(factory) - : ((global = global || self), (global.jcurl = factory())); -})(this, function() { + ? define(['exports'], factory) + : ((global = global || self), factory((global.jcurl = {}))); +})(this, function(exports) { 'use strict'; /** @@ -336,18 +336,25 @@ return url; } - - var url = { - getParams: getParams, - getParamsExtended: getParamsExtended, - addParams: addParams, - addParamsExtended: addParamsExtended, - // short aliases - get: getParams, - getExt: getParamsExtended, - add: addParams, - addExt: addParamsExtended - }; - - return url; + // getParams, + // getParamsExtended, + // addParams, + // addParamsExtended, + // // short aliases + // get: getParams, + // getExt: getParamsExtended, + // add: addParams, + // addExt: addParamsExtended + // }; + + exports.add = addParams; + exports.addExt = addParamsExtended; + exports.addParams = addParams; + exports.addParamsExtended = addParamsExtended; + exports.get = getParams; + exports.getExt = getParamsExtended; + exports.getParams = getParams; + exports.getParamsExtended = getParamsExtended; + + Object.defineProperty(exports, '__esModule', { value: true }); }); diff --git a/dist/url.min.js b/dist/url.min.js index 079cd69..b5c6a04 100644 --- a/dist/url.min.js +++ b/dist/url.min.js @@ -1 +1 @@ -(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?module.exports=b():"function"==typeof define&&define.amd?define(b):(a=a||self,a.jcurl=b())})(this,function(){'use strict';function a(a){var b={};return a&&a.split("&").forEach(function(a){var c=a.split("=",2),d=c[0],e=c[1]||"";"[]"===d.substr(-2)?(d=d.substr(0,d.length-2),(void 0===b[d]||!Array.isArray(b[d]))&&(b[d]=[]),b[d].push(decodeURIComponent(e))):b[d]=decodeURIComponent(e)}),b}function b(a,c){var d={};return d[a[a.length-1]]=c,1===a.length?d:b(a.slice(0,a.length-1),d)}function c(a,b){var d=function(a){return a&&a instanceof Object};return d(a)&&d(b)?(Object.keys(b).forEach(function(e){var f=a[e],g=b[e];a[e]=Array.isArray(f)&&Array.isArray(g)?f.concat(g):d(f)&&d(g)?c(Object.assign({},f),g):g}),a):b}function d(a){var d={};return a.split("&").forEach(function(a,e){var f=a.split("=",2),g=f[0],h=f[1]||"",i=g.match(/(.+?)(\[(.*)\])/i);if(i){var j=i[3]||e+"",k=j.split("][");k.unshift(i[1]);var l=b(k,h);d=c(d,l)}}),d}function e(a){var b=[],c=function(c){if(a.hasOwnProperty(c)){var d=a[c];Array.isArray(d)&&d.length?d.forEach(function(a){b.push("".concat(c,"[]=").concat(a))}):b.push("".concat(c,"=").concat(d))}};for(var d in a)c(d);return b.join("&")}function f(a,b,c){Object.keys(a).forEach(function(d){var e=b.concat([d]),g=a[d];g instanceof Object?f(g,e,c):(e.push(g),c.push(e))})}function g(a){var b=[];f(a,[],b);var c=b.map(function(a){return a.reduce(function(b,c,d){return b?d { test('getParams()', () => { - expect(jcurl.getParams('example.com')).toEqual({}); - expect(jcurl.getParams('example.com?bar=1&foo')).toEqual({ bar: '1', foo: '' }); - expect(jcurl.getParams('example.com?bar=1&bar=2')).toEqual({ bar: '2' }); - expect(jcurl.getParams('example.com?bar[]=1&bar[]=2')).toEqual({ bar: ['1', '2'] }); - expect(jcurl.getParams('example.com?bar=1&bar[]=2')).toEqual({ bar: ['2'] }); + expect(getParams('example.com')).toEqual({}); + expect(getParams('example.com?bar=1&foo')).toEqual({ bar: '1', foo: '' }); + expect(getParams('example.com?bar=1&bar=2')).toEqual({ bar: '2' }); + expect(getParams('example.com?bar[]=1&bar[]=2')).toEqual({ bar: ['1', '2'] }); + expect(getParams('example.com?bar=1&bar[]=2')).toEqual({ bar: ['2'] }); }); test('getParamsExtended()', () => { - expect(jcurl.getParamsExtended('example.com?bar[t]=1&bar[j]=2')).toEqual({ bar: { t: '1', j: '2' } }); - expect(jcurl.getParamsExtended('example.com?bar[t]=1&bar[j]=2&bar[j]=3')).toEqual({ bar: { t: '1', j: '3' } }); - expect(jcurl.getParamsExtended('example.com?b[t]=1&b[j]=2&b[j][g]=3')) + expect(getParamsExtended('example.com?bar[t]=1&bar[j]=2')).toEqual({ bar: { t: '1', j: '2' } }); + expect(getParamsExtended('example.com?bar[t]=1&bar[j]=2&bar[j]=3')).toEqual({ bar: { t: '1', j: '3' } }); + expect(getParamsExtended('example.com?b[t]=1&b[j]=2&b[j][g]=3')) .toEqual({ b: { t: '1', j: { g: '3' } } }); - expect(jcurl.getParamsExtended('example.com?bar=-1&bar[]=0&bar[tr]=1&bar[j]=2&bar[foo][too][poo]=3&bar[]=4&bar[foo][too][hoo]=5')) + expect(getParamsExtended('example.com?bar=-1&bar[]=0&bar[tr]=1&bar[j]=2&bar[foo][too][poo]=3&bar[]=4&bar[foo][too][hoo]=5')) .toEqual({ bar: { '1': '0', tr: '1', j: '2', foo: { too: { poo: '3', hoo: '5' } }, '5': '4' } }); }); }); describe('ADD', () => { test('addParams()', () => { - expect(jcurl.addParams('example.com', { bar: 1 })).toBe('example.com?bar=1'); - expect(jcurl.addParams('example.com', { bar: 1, foo: 2 })).toBe('example.com?bar=1&foo=2'); - expect(jcurl.addParams('example.com?bar=1&foo', { bar: 2 })).toBe('example.com?bar=2&foo='); - expect(jcurl.addParams('example.com?bar=1&foo', { bar: 2, foo: 2 })).toBe('example.com?bar=2&foo=2'); - expect(jcurl.addParams('example.com?bar=1', { bar: [2, 3] })).toBe('example.com?bar[]=2&bar[]=3'); - expect(jcurl.addParams('example.com?bar=1&bar[]=2', { bar: [3, 4] })).toBe('example.com?bar[]=2&bar[]=3&bar[]=4'); - expect(jcurl.addParams('example.com?bar=1&bar=2', { bar: [3, 4] })).toBe('example.com?bar[]=3&bar[]=4'); - expect(jcurl.addParams('example.com?bar[]=1&bar[]=2', { bar: [3, 4] })).toBe('example.com?bar[]=1&bar[]=2&bar[]=3&bar[]=4'); + expect(addParams('example.com', { bar: 1 })).toBe('example.com?bar=1'); + expect(addParams('example.com', { bar: 1, foo: 2 })).toBe('example.com?bar=1&foo=2'); + expect(addParams('example.com?bar=1&foo', { bar: 2 })).toBe('example.com?bar=2&foo='); + expect(addParams('example.com?bar=1&foo', { bar: 2, foo: 2 })).toBe('example.com?bar=2&foo=2'); + expect(addParams('example.com?bar=1', { bar: [2, 3] })).toBe('example.com?bar[]=2&bar[]=3'); + expect(addParams('example.com?bar=1&bar[]=2', { bar: [3, 4] })).toBe('example.com?bar[]=2&bar[]=3&bar[]=4'); + expect(addParams('example.com?bar=1&bar=2', { bar: [3, 4] })).toBe('example.com?bar[]=3&bar[]=4'); + expect(addParams('example.com?bar[]=1&bar[]=2', { bar: [3, 4] })).toBe('example.com?bar[]=1&bar[]=2&bar[]=3&bar[]=4'); }); test('addParamsExtended()', () => { - expect(jcurl.addParamsExtended('example.com', { bar: 1, foo: 2 })).toBe('example.com?bar=1&foo=2'); + expect(addParamsExtended('example.com', { bar: 1, foo: 2 })).toBe('example.com?bar=1&foo=2'); /** * BUG */ - // expect(jcurl.addParamsExtended('example.com?bar[foo]=test&bar[joo]=2', { bar: { foo: 'new', too: 5 } })) + // expect(addParamsExtended('example.com?bar[foo]=test&bar[joo]=2', { bar: { foo: 'new', too: 5 } })) // .toBe('example.com?bar[foo]=new&bar[joo]=2&bar[too]=5'); - expect(jcurl.addParamsExtended('example.com', { bar: { foo: 'test', joo: 2 } })) + expect(addParamsExtended('example.com', { bar: { foo: 'test', joo: 2 } })) .toBe('example.com?bar[foo]=test&bar[joo]=2'); - expect(jcurl.addParamsExtended('example.com', { + expect(addParamsExtended('example.com', { f1: { f11: 1, f12: 2, f13: 3 }, f2: 4, f3: { f31: { f311: 6, f312: { f3121: 9 } }, f32: { f321: 2 } }, f4: { f41: 3, f42: { f421: 2 } }, f5: [2, 4] })).toBe('example.com?f1[f11]=1&f1[f12]=2&f1[f13]=3&f2=4&f3[f31][f311]=6&f3[f31][f312][f3121]=9&f3[f32][f321]=2&f4[f41]=3&f4[f42][f421]=2&f5[0]=2&f5[1]=4');