imgix-url-builder 0.0.2 → 0.0.3

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.
Files changed (2) hide show
  1. package/dist/index.js +55 -0
  2. package/package.json +2 -1
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ const camelCaseToParamCase = (input) => {
2
+ return input.replace(/[A-Z]/g, (match) => {
3
+ return `-${match.toLowerCase()}`;
4
+ });
5
+ };
6
+ const buildURL = (url, params) => {
7
+ const instance = new URL(url);
8
+ for (const camelCasedParamKey in params) {
9
+ const paramKey = camelCaseToParamCase(camelCasedParamKey);
10
+ const paramValue = params[camelCasedParamKey];
11
+ if (paramValue === void 0) {
12
+ instance.searchParams.delete(paramKey);
13
+ } else if (Array.isArray(paramValue)) {
14
+ instance.searchParams.set(paramKey, paramValue.join(","));
15
+ } else {
16
+ instance.searchParams.set(paramKey, `${paramValue}`);
17
+ }
18
+ }
19
+ const s = instance.searchParams.get("s");
20
+ if (s) {
21
+ instance.searchParams.delete("s");
22
+ instance.searchParams.append("s", s);
23
+ }
24
+ return instance.toString();
25
+ };
26
+
27
+ const buildPixelDensitySrcSet = (url, { pixelDensities, ...params }) => {
28
+ return pixelDensities.map((dpr) => {
29
+ return `${buildURL(url, { ...params, dpr })} ${dpr}x`;
30
+ }).join(", ");
31
+ };
32
+
33
+ const buildWidthSrcSet = (url, { widths, ...params }) => {
34
+ return widths.map((width) => {
35
+ return `${buildURL(url, { ...params, w: void 0, width })} ${width}w`;
36
+ }).join(", ");
37
+ };
38
+
39
+ class Client {
40
+ constructor(options) {
41
+ this.baseURL = options.baseURL;
42
+ }
43
+ buildURLForPath(path, params = {}) {
44
+ return buildURL(`${new URL(path, this.baseURL)}`, params);
45
+ }
46
+ buildWidthSrcSetForPath(path, params) {
47
+ return buildWidthSrcSet(`${new URL(path, this.baseURL)}`, params);
48
+ }
49
+ buildPixelDensitySrcSetForPath(path, params) {
50
+ return buildPixelDensitySrcSet(`${new URL(path, this.baseURL)}`, params);
51
+ }
52
+ }
53
+
54
+ export { Client, buildPixelDensitySrcSet, buildURL, buildWidthSrcSet };
55
+ //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imgix-url-builder",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "JavaScript/TypeScript Imgix URL builders for browsers and Node.js",
5
5
  "keywords": [
6
6
  "typescript",
@@ -27,6 +27,7 @@
27
27
  },
28
28
  "main": "dist/index.cjs",
29
29
  "module": "dist/index.mjs",
30
+ "react-native": "dist/index.js",
30
31
  "types": "dist/index.d.ts",
31
32
  "files": [
32
33
  "dist",