@chriscdn/build-url 1.0.12 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,7 +8,7 @@ This package is a fork of [@googlicius/build-url](https://github.com/googlicius/
8
8
 
9
9
  I forked this package for three reasons:
10
10
 
11
- - to provide an ES6 build,
11
+ - to provide an ESM build,
12
12
  - to fix [this issue](https://github.com/googlicius/build-url/issues/3), and
13
13
  - to provide a named export.
14
14
 
@@ -30,7 +30,7 @@ yarn add @chriscdn/build-url
30
30
 
31
31
  Create a url:
32
32
 
33
- ```js
33
+ ```ts
34
34
  import { buildUrl } from "@chriscdn/build-url";
35
35
 
36
36
  buildUrl("http://my-website.com/post", {
@@ -44,7 +44,7 @@ buildUrl("http://my-website.com/post", {
44
44
 
45
45
  Add another query parameter:
46
46
 
47
- ```js
47
+ ```ts
48
48
  buildUrl("http://my-website.com/post?page=2", {
49
49
  queryParams: {
50
50
  sort: "title:asc",
@@ -56,7 +56,7 @@ buildUrl("http://my-website.com/post?page=2", {
56
56
 
57
57
  Input url/path is omitted:
58
58
 
59
- ```js
59
+ ```ts
60
60
  buildUrl({
61
61
  queryParams: {
62
62
  sort: "title:asc",
@@ -68,7 +68,7 @@ buildUrl({
68
68
 
69
69
  Remove a query parameter:
70
70
 
71
- ```js
71
+ ```ts
72
72
  buildUrl("images?page=2&sort=title:asc", {
73
73
  queryParams: {
74
74
  page: null,
@@ -80,7 +80,7 @@ buildUrl("images?page=2&sort=title:asc", {
80
80
 
81
81
  Return an absolute url:
82
82
 
83
- ```js
83
+ ```ts
84
84
  // Assume that current url is: http://awesome-website.com
85
85
 
86
86
  buildUrl("/posts", {
@@ -95,4 +95,4 @@ buildUrl("/posts", {
95
95
 
96
96
  ## License
97
97
 
98
- MIT
98
+ [MIT](LICENSE)
package/package.json CHANGED
@@ -1,26 +1,14 @@
1
1
  {
2
2
  "name": "@chriscdn/build-url",
3
- "version": "1.0.12",
3
+ "version": "2.0.0",
4
4
  "description": "A small utility for building URLs.",
5
5
  "repository": "https://github.com/chriscdn/build-url",
6
6
  "author": "Christopher Meyer <chris@schwiiz.org>",
7
7
  "license": "MIT",
8
8
  "type": "module",
9
- "main": "./lib/index.cjs",
10
- "module": "./lib/index.js",
9
+ "main": "./lib/index.js",
11
10
  "types": "./lib/index.d.ts",
12
- "exports": {
13
- ".": {
14
- "import": {
15
- "types": "./lib/index.d.ts",
16
- "default": "./lib/index.js"
17
- },
18
- "require": {
19
- "types": "./lib/index.d.cts",
20
- "default": "./lib/index.cjs"
21
- }
22
- }
23
- },
11
+ "exports": "./lib/index.js",
24
12
  "scripts": {
25
13
  "build": "tsup",
26
14
  "watch": "yarn build --watch",
package/lib/index.cjs DELETED
@@ -1,72 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- buildUrl: () => buildUrl
24
- });
25
- module.exports = __toCommonJS(index_exports);
26
- var isEmpty = (value) => value === null || value === void 0;
27
- var buildUrl = (inputUrl, options) => {
28
- let url;
29
- let isValidInputUrl = false;
30
- try {
31
- url = new URL(inputUrl);
32
- } catch (error) {
33
- isValidInputUrl = true;
34
- if (typeof inputUrl === "string") {
35
- const host = typeof window === "undefined" ? "http://example.com" : window.location.origin;
36
- url = new URL(`${host}/${inputUrl.replace(/^\/|\/$/g, "")}`);
37
- } else {
38
- url = typeof window === "undefined" ? new URL("http://example.com") : new URL(window.location.href);
39
- }
40
- }
41
- const _options = typeof inputUrl === "string" ? options : inputUrl;
42
- if (_options?.queryParams) {
43
- for (const key in _options.queryParams) {
44
- if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {
45
- const element = _options.queryParams[key];
46
- if (isEmpty(element)) {
47
- url.searchParams.delete(key);
48
- } else {
49
- url.searchParams.set(key, element);
50
- }
51
- }
52
- }
53
- }
54
- if (_options?.path) {
55
- url.pathname = _options.path;
56
- }
57
- if (_options?.path === null) {
58
- url.pathname = "";
59
- }
60
- if (_options?.hash) {
61
- url.hash = _options.hash;
62
- }
63
- if (isValidInputUrl && !_options?.returnAbsoluteUrl) {
64
- return url.pathname + url.search + url.hash;
65
- }
66
- return url.toString();
67
- };
68
- // Annotate the CommonJS export names for ESM import in node:
69
- 0 && (module.exports = {
70
- buildUrl
71
- });
72
- //# sourceMappingURL=index.cjs.map
package/lib/index.cjs.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type UrlOptions = {\n queryParams?: {\n [x: string]: any;\n };\n hash?: string;\n path?: string | null;\n returnAbsoluteUrl?: boolean;\n};\n\nconst isEmpty = (value: any) => value === null || value === undefined;\n\nconst buildUrl = (\n inputUrl?: string | UrlOptions,\n options?: UrlOptions,\n) => {\n let url: URL;\n let isValidInputUrl = false;\n\n try {\n url = new URL(inputUrl as string);\n } catch (error) {\n isValidInputUrl = true;\n\n if (typeof inputUrl === \"string\") {\n const host = typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url = typeof window === \"undefined\"\n ? new URL(\"http://example.com\")\n : new URL(window.location.href);\n }\n }\n\n const _options = typeof inputUrl === \"string\" ? options : inputUrl;\n\n if (_options?.queryParams) {\n for (const key in _options.queryParams) {\n if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {\n const element = _options.queryParams[key];\n\n if (isEmpty(element)) {\n url.searchParams.delete(key);\n } else {\n url.searchParams.set(key, element);\n }\n }\n }\n }\n\n if (_options?.path) {\n url.pathname = _options.path;\n }\n\n if (_options?.path === null) {\n url.pathname = \"\";\n }\n\n if (_options?.hash) {\n url.hash = _options.hash;\n }\n\n if (isValidInputUrl && !_options?.returnAbsoluteUrl) {\n return url.pathname + url.search + url.hash;\n }\n\n return url.toString();\n};\n\nexport { buildUrl };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,IAAM,UAAU,CAAC,UAAe,UAAU,QAAQ,UAAU;AAE5D,IAAM,WAAW,CACf,UACA,YACG;AACH,MAAI;AACJ,MAAI,kBAAkB;AAEtB,MAAI;AACF,UAAM,IAAI,IAAI,QAAkB;AAAA,EAClC,SAAS,OAAO;AACd,sBAAkB;AAElB,QAAI,OAAO,aAAa,UAAU;AAChC,YAAM,OAAO,OAAO,WAAW,cAC3B,uBACA,OAAO,SAAS;AAEpB,YAAM,IAAI,IAAI,GAAG,IAAI,IAAI,SAAS,QAAQ,YAAY,EAAE,CAAC,EAAE;AAAA,IAC7D,OAAO;AACL,YAAM,OAAO,WAAW,cACpB,IAAI,IAAI,oBAAoB,IAC5B,IAAI,IAAI,OAAO,SAAS,IAAI;AAAA,IAClC;AAAA,EACF;AAEA,QAAM,WAAW,OAAO,aAAa,WAAW,UAAU;AAE1D,MAAI,UAAU,aAAa;AACzB,eAAW,OAAO,SAAS,aAAa;AACtC,UAAI,OAAO,UAAU,eAAe,KAAK,SAAS,aAAa,GAAG,GAAG;AACnE,cAAM,UAAU,SAAS,YAAY,GAAG;AAExC,YAAI,QAAQ,OAAO,GAAG;AACpB,cAAI,aAAa,OAAO,GAAG;AAAA,QAC7B,OAAO;AACL,cAAI,aAAa,IAAI,KAAK,OAAO;AAAA,QACnC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,MAAI,UAAU,MAAM;AAClB,QAAI,WAAW,SAAS;AAAA,EAC1B;AAEA,MAAI,UAAU,SAAS,MAAM;AAC3B,QAAI,WAAW;AAAA,EACjB;AAEA,MAAI,UAAU,MAAM;AAClB,QAAI,OAAO,SAAS;AAAA,EACtB;AAEA,MAAI,mBAAmB,CAAC,UAAU,mBAAmB;AACnD,WAAO,IAAI,WAAW,IAAI,SAAS,IAAI;AAAA,EACzC;AAEA,SAAO,IAAI,SAAS;AACtB;","names":[]}
package/lib/index.d.cts DELETED
@@ -1,11 +0,0 @@
1
- type UrlOptions = {
2
- queryParams?: {
3
- [x: string]: any;
4
- };
5
- hash?: string;
6
- path?: string | null;
7
- returnAbsoluteUrl?: boolean;
8
- };
9
- declare const buildUrl: (inputUrl?: string | UrlOptions, options?: UrlOptions) => string;
10
-
11
- export { type UrlOptions, buildUrl };