Comparing version
@@ -10,2 +10,16 @@ # Changelog | ||
## [1.0.5] - 2022-11-21 | ||
### Added | ||
- `replace` method to query & filters parameters | ||
- `toggle` method to filters parameters | ||
- `append` method to filters parameters | ||
- Functional `or` & `and` methods in filters parameters | ||
### Changed | ||
- `append` using encoded value instead of `QueryParameter.rawValue` (added raw options) | ||
- `append` triggering error when no value when indexes were only needed | ||
## [1.0.4] - 2022-11-20 | ||
@@ -12,0 +26,0 @@ |
import { type FlexibleUrl } from './flex-url.js'; | ||
import { type QueryParameterManipulator, type QueryParameterModifiers } from './query-params.js'; | ||
import { QueryParameterManipulator, type QueryParameterModifiers } from './query-params.js'; | ||
export type FilterParameterConditional = 'and' | 'or' | undefined; | ||
@@ -7,3 +7,15 @@ export declare class FilterParameterChecker { | ||
constructor(flexUrl: FlexibleUrl); | ||
has(filterKey: string, value?: string, modifiers?: QueryParameterModifiers): boolean; | ||
static find(flexUrl: FlexibleUrl, filterKey: string, values: string[], modifiers?: QueryParameterModifiers, strict?: boolean): number[]; | ||
/** | ||
* Check if filter parameter exists in URL containing value(s). | ||
* | ||
* @see Docs https://flex-url.opensoutheners.com/docs/filters#includes | ||
*/ | ||
includes(filterKey: string, values: string | string[], modifiers?: QueryParameterModifiers): boolean; | ||
/** | ||
* Check if filter parameter exists in URL with value(s). | ||
* | ||
* @see Docs https://flex-url.opensoutheners.com/docs/filters#has | ||
*/ | ||
has(filterKey: string, values?: string | string[], modifiers?: QueryParameterModifiers, strict?: boolean): boolean; | ||
} | ||
@@ -20,2 +32,3 @@ export declare class FilterParameterManipulator { | ||
set(value: string, modifiers?: QueryParameterModifiers): FilterParameterManipulator; | ||
replace(value: string | ((oldValue: string) => string)): FilterParameterManipulator; | ||
append(value: string): FilterParameterManipulator; | ||
@@ -22,0 +35,0 @@ remove(value: string, modifiers?: QueryParameterModifiers): FlexibleUrl; |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t||self).flexUrl={})}(this,function(t){function e(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function n(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}var i=0;function r(t){return"__private_"+i+++"_"+t}function o(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var a=/*#__PURE__*/function(){function t(t){this.flexUrl=void 0,this.flexUrl=t,this.flexUrl=t}return t.prototype.has=function(t,e,n){return void 0===n&&(n=[]),this.flexUrl.queryParams.has("filter",e,[t].concat(n))},t}(),s=/*#__PURE__*/function(){function t(t,e){this.manipulator=void 0,this.filterKey=void 0,this.conditional=void 0,this.manipulator=t,this.filterKey=e,this.manipulator=t,this.filterKey=e}t.fromUrl=function(e,n){return new t(e.queryParam("filter"),n)};var e=t.prototype;return e.add=function(e,n){return void 0===n&&(n=[]),new t(this.manipulator.add(e,[this.filterKey].concat(n)),this.filterKey)},e.set=function(e,n){return void 0===n&&(n=[]),new t(this.manipulator.set(e,[this.filterKey].concat(n)),this.filterKey)},e.append=function(e){return new t(this.manipulator.append(e),this.filterKey)},e.remove=function(t,e){return void 0===e&&(e=[]),this.manipulator.remove(t,[this.filterKey].concat(e))},e.toggle=function(e,n){return void 0===n&&(n=[]),new t(this.manipulator.toggle(e,[this.filterKey].concat(n)),this.filterKey)},n(t,[{key:"or",get:function(){return this.conditional="or",this}},{key:"and",get:function(){return this.conditional="and",this}}]),t}();function u(t,e,n){var i=-1;for(i=0;i<t.length;i++)e.includes(i)&&(t[i]="function"==typeof n?n(t[i]):n)}var f=/*#__PURE__*/r("name"),l=/*#__PURE__*/r("value"),h=/*#__PURE__*/r("modifiers"),d=/*#__PURE__*/function(){function t(t,e,n){void 0===n&&(n=[]),Object.defineProperty(this,f,{writable:!0,value:void 0}),Object.defineProperty(this,l,{writable:!0,value:void 0}),Object.defineProperty(this,h,{writable:!0,value:void 0}),o(this,f)[f]=t,o(this,l)[l]=e,o(this,h)[h]=n}t.queryParamKey=function(t,e){return void 0===e&&(e=[]),""+t+e.map(function(t){return"["+t+"]"}).join("")};var e=t.prototype;return e.setValue=function(t){return o(this,l)[l]=t,this},e.setModifiers=function(t){return o(this,h)[h]=t,this},e.toString=function(){return this.queryParamKey+"="+this.value},t.fromString=function(e){var n=decodeURIComponent(e).split("="),i=n[0],r=n[1],o=i,a=[];if(i.includes("[")){var s=i.split(/\[|\|/);o=s.shift(),a=s.map(function(t){return t.replace("]","")})}if(o)return new t(o,r,a)},n(t,[{key:"name",get:function(){return encodeURIComponent(o(this,f)[f])}},{key:"value",get:function(){return encodeURIComponent(o(this,l)[l])}},{key:"modifiers",get:function(){return o(this,h)[h].map(function(t){return encodeURIComponent(t)})}},{key:"queryParamKey",get:function(){return t.queryParamKey(this.name,this.modifiers)}}]),t}(),c=/*#__PURE__*/function(){function t(t){this.flexUrl=void 0,this.flexUrl=t,this.flexUrl=t}return t.find=function(t,e,n,i){return t.params.findIndex(function(t){return t.queryParamKey===d.queryParamKey(e,i)&&(!n||t.value===n)})},t.prototype.has=function(e,n,i){return-1!==t.find(this.flexUrl,e,n,i)},t}(),p=/*#__PURE__*/function(){function t(t,e,n,i,r){void 0===i&&(i=[]),void 0===r&&(r=[]),this.flexUrl=void 0,this.name=void 0,this.value=void 0,this.indexes=void 0,this.flexUrl=t,this.name=e,this.value=n,this.indexes=r,this.flexUrl=t,this.name=e,this.value=n,this.indexes=r.length>0?r:function(t,e){var n=[],i=-1;for(i=0;i<t.length;i++)e(t[i])&&n.push(i);return n}(t.params,function(t){return t.queryParamKey===d.queryParamKey(e,i)&&(!n||t.value===n)})}t.fromIndexes=function(e,n,i){return new t(e,n,void 0,[],i)};var e=t.prototype;return e.set=function(t,e){return void 0===e&&(e=[]),u(this.flexUrl.params,this.indexes,new d(this.name,t,e)),this},e.add=function(e,n){void 0===n&&(n=[]);var i=c.find(this.flexUrl,this.name,e,n);return-1!==i?t.fromIndexes(this.flexUrl,this.name,[i]):(this.flexUrl.params.push(new d(this.name,e,n)),new t(this.flexUrl,this.name,e,n))},e.append=function(t){var e;if(!(this.value||null!=(e=this.indexes)&&e.length))throw new Error("Query parameter value must be provided to append to the right parameter.");return u(this.flexUrl.params,this.indexes,function(e){return e.setValue(""+e.value+t)}),this},e.toggle=function(t,e){void 0===e&&(e=[]);var n=c.find(this.flexUrl,this.name,t,e);return-1!==n?(this.remove(t,e,n),this):this.add(t,e)},e.remove=function(t,e,n){void 0===e&&(e=[]);var i=null!=n?n:c.find(this.flexUrl,this.name,t,e);return-1!==i&&(delete this.flexUrl.params[i],this.flexUrl.params=this.flexUrl.params.filter(Boolean)),this.flexUrl},e.withModifiers=function(t){return u(this.flexUrl.params,this.indexes,function(e){return e.setModifiers(t)}),this},t}(),m=/*#__PURE__*/function(){function t(t){this.params=[],this.baseUrl=void 0,this.hashFragment=void 0;var e="string"==typeof t?new URL(t):t;if(e||"undefined"==typeof window||(e=window.location),!e)throw new Error("Need to provide a valid URL");this.hashFragment=e.hash,this.baseUrl=e.origin;for(var n=e.search.replace("?","").split("&"),i=0;i<n.length;i++){var r=d.fromString(n[i]);r&&this.params.push(r)}}var e=t.prototype;return e.queryParam=function(t,e){return new p(this,t,e)},e.filter=function(t){return s.fromUrl(this,t)},e.clear=function(){return this.params=[],this},e.toString=function(){return[this.baseUrl,this.params.map(function(t){return t.toString()}).join("&")].filter(Boolean).join("?")+this.hashFragment},n(t,[{key:"queryParams",get:function(){return new c(this)}},{key:"filters",get:function(){return new a(this)}}]),t}();t.FlexibleUrl=m,t.flexUrl=function(t){return new m(t)}}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t||self).flexUrl={})}(this,function(t){function e(t,e){for(var i=0;i<e.length;i++){var r=e[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function i(t,i,r){return i&&e(t.prototype,i),r&&e(t,r),t}var r=0;function n(t){return"__private_"+r+++"_"+t}function o(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}function a(t,e){var i=[],r=-1;for(r=0;r<t.length;r++)("function"==typeof e?e(t[r]):e)&&i.push(r);return i}function s(t,e,i){var r=-1;for(r=0;r<t.length;r++)e.includes(r)&&(t[r]="function"==typeof i?i(t[r]):i)}var u=/*#__PURE__*/n("name"),l=/*#__PURE__*/n("value"),f=/*#__PURE__*/n("modifiers"),h=/*#__PURE__*/function(){function t(t,e,i){void 0===i&&(i=[]),Object.defineProperty(this,u,{writable:!0,value:void 0}),Object.defineProperty(this,l,{writable:!0,value:void 0}),Object.defineProperty(this,f,{writable:!0,value:void 0}),o(this,u)[u]=t,o(this,l)[l]=e,o(this,f)[f]=i}t.queryParamKey=function(t,e){return void 0===e&&(e=[]),""+t+e.map(function(t){return"["+t+"]"}).join("")};var e=t.prototype;return e.setValue=function(t){return o(this,l)[l]=t,this},e.setModifiers=function(t){return o(this,f)[f]=t,this},e.toString=function(){return t.queryParamKey(this.name,this.modifiers)+"="+this.value},t.fromString=function(e){var i=decodeURIComponent(e).split("="),r=i[0],n=i[1],o=r,a=[];if(r.includes("[")){var s=r.split(/\[|\|/);o=s.shift(),a=s.map(function(t){return t.replace("]","")})}if(o)return new t(o,n,a)},i(t,[{key:"name",get:function(){return encodeURIComponent(o(this,u)[u])}},{key:"value",get:function(){return encodeURIComponent(o(this,l)[l])}},{key:"rawValue",get:function(){return o(this,l)[l]}},{key:"modifiers",get:function(){return o(this,f)[f].map(function(t){return encodeURIComponent(t)})}},{key:"queryParamKey",get:function(){return t.queryParamKey(o(this,u)[u],o(this,f)[f])}}]),t}(),c=/*#__PURE__*/function(){function t(t){this.flexUrl=void 0,this.flexUrl=t,this.flexUrl=t}return t.find=function(t,e,i,r){return t.params.findIndex(function(t){return t.queryParamKey===h.queryParamKey(e,r)&&(!i||t.value.toLocaleLowerCase()===i.toLocaleLowerCase())})},t.prototype.has=function(e,i,r){return-1!==t.find(this.flexUrl,e,i,r)},t}(),d=/*#__PURE__*/function(){function t(t,e,i,r,n){void 0===r&&(r=[]),void 0===n&&(n=[]),this.flexUrl=void 0,this.name=void 0,this.value=void 0,this.modifiers=void 0,this.indexes=void 0,this.flexUrl=t,this.name=e,this.value=i,this.modifiers=r,this.indexes=n,0===n.length&&(this.indexes=a(t.params,function(t){return t.queryParamKey===h.queryParamKey(e,r)&&(!i||t.value===i)}))}t.fromIndexes=function(e,i,r,n){return void 0===n&&(n=[]),new t(e,i,void 0,n,r)};var e=t.prototype;return e.set=function(t,e){return void 0===e&&(e=[]),s(this.flexUrl.params,this.indexes,new h(this.name,t,e)),this},e.add=function(e,i){void 0===i&&(i=[]);var r=c.find(this.flexUrl,this.name,e,i);return-1!==r?t.fromIndexes(this.flexUrl,this.name,[r]):(this.flexUrl.params.push(new h(this.name,e,i)),new t(this.flexUrl,this.name,e,i))},e.replace=function(t){var e;if(null==(e=this.indexes)||!e.length)throw new Error("Query parameter must be provided to replace to the right parameter.");return s(this.flexUrl.params,this.indexes,function(e){return e.setValue("function"==typeof t?t(e.rawValue):t)}),this},e.append=function(t){return this.replace(function(e){return""+e+t})},e.toggle=function(t,e){void 0===e&&(e=[]);var i=c.find(this.flexUrl,this.name,t,e);return-1!==i?(this.remove(t,e,i),this):this.add(t,e)},e.remove=function(t,e,i){void 0===e&&(e=[]);var r=null!=i?i:c.find(this.flexUrl,this.name,t,e);return-1!==r&&(delete this.flexUrl.params[r],this.flexUrl.params=this.flexUrl.params.filter(Boolean)),this.flexUrl},e.withModifiers=function(t){return s(this.flexUrl.params,this.indexes,function(e){return e.setModifiers(t)}),this},i(t,[{key:"url",get:function(){return this.flexUrl}},{key:"exists",get:function(){var t=this;return this.indexes.length>0&&this.indexes.every(function(e,i){var r;return Boolean(null==(r=t.flexUrl.params)?void 0:r[i])})}}]),t}(),p=/*#__PURE__*/function(){function t(t){this.flexUrl=void 0,this.flexUrl=t}t.find=function(t,e,i,r,n){void 0===r&&(r=[]),void 0===n&&(n=!0);var o=i.join(",").toLocaleLowerCase().split(",");return a(t.params,function(t){if(!t.rawValue.includes(","))return!1;if(t.queryParamKey===h.queryParamKey("filter",[e].concat(r))){var i=t.rawValue.toLocaleLowerCase().split(",");return n?i.every(function(t){return o.includes(t)}):i.some(function(t){return o.includes(t)})}return!1})};var e=t.prototype;return e.includes=function(t,e,i){return void 0===i&&(i=[]),this.has(t,e,i,!1)},e.has=function(e,i,r,n){return void 0===r&&(r=[]),void 0===n&&(n=!0),Array.isArray(i)?t.find(this.flexUrl,e,i,r,n).length>0:this.flexUrl.queryParams.has("filter",i,[e].concat(r))},t}(),m=/*#__PURE__*/function(){function t(t,e){this.manipulator=void 0,this.filterKey=void 0,this.conditional="and",this.manipulator=t,this.filterKey=e}t.fromUrl=function(e,i){return new t(e.queryParam("filter"),i)};var e=t.prototype;return e.add=function(e,i){return void 0===i&&(i=[]),"or"===this.conditional&&this.manipulator.url.filters.has(this.filterKey)&&!this.manipulator.url.filters.includes(this.filterKey,[e],i)?this.append(","+e):new t(this.manipulator.add(e,[this.filterKey].concat(i)),this.filterKey)},e.set=function(e,i){return void 0===i&&(i=[]),new t(this.manipulator.set(e,[this.filterKey].concat(i)),this.filterKey)},e.replace=function(e){return new t(this.manipulator.replace(e),this.filterKey)},e.append=function(e){return new t(this.manipulator.append(e),this.filterKey)},e.remove=function(t,e){return void 0===e&&(e=[]),this.manipulator.remove(t,[this.filterKey].concat(e))},e.toggle=function(e,i){if(void 0===i&&(i=[]),"or"===this.conditional){var r=p.find(this.manipulator.url,this.filterKey,[e],i);if(r.length>0)return this.set(this.manipulator.url.params[r[0]].value.split(",").filter(function(t){return t!==e}).join(","));if(this.manipulator.url.filters.has(this.filterKey)){var n=this.manipulator.exists?this:new t(d.fromIndexes(this.manipulator.url,"filter",r,[this.filterKey].concat(i)),this.filterKey);return this.manipulator.url.filters.includes(this.filterKey,[e])?n.replace(function(t){return t.split(",").filter(function(t){return t.toLocaleLowerCase()!==e.toLocaleLowerCase()}).join(",")}):n.append(","+e)}return this.add(e)}return new t(this.manipulator.toggle(e,[this.filterKey].concat(i)),this.filterKey)},i(t,[{key:"or",get:function(){return this.conditional="or",this}},{key:"and",get:function(){return this.conditional="and",this}}]),t}(),v=/*#__PURE__*/function(){function t(t){this.params=[],this.baseUrl=void 0,this.hashFragment=void 0;var e="string"==typeof t?new URL(t):t;if(e||"undefined"==typeof window||(e=window.location),!e)throw new Error("Need to provide a valid URL");this.hashFragment=e.hash,this.baseUrl=e.origin;for(var i=e.search.replace("?","").split("&"),r=0;r<i.length;r++){var n=h.fromString(i[r]);n&&this.params.push(n)}}var e=t.prototype;return e.queryParam=function(t,e){return new d(this,t,e)},e.filter=function(t){return m.fromUrl(this,t)},e.clear=function(){return this.params=[],this},e.toString=function(){return[this.baseUrl,this.params.map(function(t){return t.toString()}).join("&")].filter(Boolean).join("?")+this.hashFragment},i(t,[{key:"queryParams",get:function(){return new c(this)}},{key:"filters",get:function(){return new p(this)}}]),t}();t.FlexibleUrl=v,t.flexUrl=function(t){return new v(t)}}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -11,2 +11,3 @@ import { type FlexibleUrl } from './flex-url.js'; | ||
get value(): string; | ||
get rawValue(): string; | ||
get modifiers(): string[]; | ||
@@ -27,6 +28,18 @@ get queryParamKey(): string; | ||
private readonly value?; | ||
private readonly modifiers; | ||
private readonly indexes; | ||
constructor(flexUrl: FlexibleUrl, name: string, value?: string | undefined, modifiers?: QueryParameterModifiers, indexes?: number[]); | ||
static fromIndexes(flexUrl: FlexibleUrl, name: string, indexes: number[]): QueryParameterManipulator; | ||
/** | ||
* Create instance of QueryParamaterManipulator from known parameters indexes. | ||
*/ | ||
static fromIndexes(flexUrl: FlexibleUrl, name: string, indexes: number[], modifiers?: QueryParameterModifiers): QueryParameterManipulator; | ||
/** | ||
* Get underlying Flex URL instance. | ||
*/ | ||
get url(): FlexibleUrl; | ||
/** | ||
* Get if manipulator contains existing parameters. | ||
*/ | ||
get exists(): boolean; | ||
/** | ||
* Add query parameter with value and/or modifiers. | ||
@@ -44,2 +57,8 @@ * | ||
/** | ||
* Replace value to query parameter. | ||
* | ||
* @see Docs https://flex-url.opensoutheners.com/docs/queryParams#replace | ||
*/ | ||
replace(replacement: string | ((oldValue: string) => string)): this; | ||
/** | ||
* Append value to query parameter. | ||
@@ -46,0 +65,0 @@ * |
{ | ||
"name": "flex-url", | ||
"description": "Zero-dependency utility for parsing and modifying URL params", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"type": "module", | ||
@@ -6,0 +6,0 @@ "source": "src/index.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
110906
28.26%264
25.12%