@giphy/js-util 4.4.2 → 5.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.
Files changed (58) hide show
  1. package/dist/index.cjs +365 -0
  2. package/dist/index.d.ts +79 -8
  3. package/dist/index.js +320 -41
  4. package/package.json +9 -9
  5. package/src/__tests__/gif-utils.test.ts +3 -10
  6. package/dist/bestfit.d.ts +0 -18
  7. package/dist/bestfit.js +0 -61
  8. package/dist/bestfit.js.map +0 -1
  9. package/dist/collections.d.ts +0 -5
  10. package/dist/collections.js +0 -42
  11. package/dist/collections.js.map +0 -1
  12. package/dist/get-client-rect-from-el.d.ts +0 -6
  13. package/dist/get-client-rect-from-el.js +0 -42
  14. package/dist/get-client-rect-from-el.js.map +0 -1
  15. package/dist/get-pingback-id.d.ts +0 -2
  16. package/dist/get-pingback-id.js +0 -45
  17. package/dist/get-pingback-id.js.map +0 -1
  18. package/dist/gif-utils.d.ts +0 -23
  19. package/dist/gif-utils.js +0 -125
  20. package/dist/gif-utils.js.map +0 -1
  21. package/dist/index.js.map +0 -1
  22. package/dist/log.d.ts +0 -15
  23. package/dist/log.js +0 -65
  24. package/dist/log.js.map +0 -1
  25. package/dist/sdk-headers.d.ts +0 -5
  26. package/dist/sdk-headers.js +0 -19
  27. package/dist/sdk-headers.js.map +0 -1
  28. package/dist/webp-check.d.ts +0 -2
  29. package/dist/webp-check.js +0 -22
  30. package/dist/webp-check.js.map +0 -1
  31. package/esm/bestfit.d.ts +0 -18
  32. package/esm/bestfit.js +0 -57
  33. package/esm/bestfit.js.map +0 -1
  34. package/esm/collections.d.ts +0 -5
  35. package/esm/collections.js +0 -34
  36. package/esm/collections.js.map +0 -1
  37. package/esm/get-client-rect-from-el.d.ts +0 -6
  38. package/esm/get-client-rect-from-el.js +0 -40
  39. package/esm/get-client-rect-from-el.js.map +0 -1
  40. package/esm/get-pingback-id.d.ts +0 -2
  41. package/esm/get-pingback-id.js +0 -43
  42. package/esm/get-pingback-id.js.map +0 -1
  43. package/esm/gif-utils.d.ts +0 -23
  44. package/esm/gif-utils.js +0 -112
  45. package/esm/gif-utils.js.map +0 -1
  46. package/esm/index.d.ts +0 -8
  47. package/esm/index.js +0 -9
  48. package/esm/index.js.map +0 -1
  49. package/esm/log.d.ts +0 -15
  50. package/esm/log.js +0 -62
  51. package/esm/log.js.map +0 -1
  52. package/esm/sdk-headers.d.ts +0 -5
  53. package/esm/sdk-headers.js +0 -13
  54. package/esm/sdk-headers.js.map +0 -1
  55. package/esm/webp-check.d.ts +0 -2
  56. package/esm/webp-check.js +0 -19
  57. package/esm/webp-check.js.map +0 -1
  58. package/src/__mocks__/webp-check.ts +0 -2
package/dist/index.js CHANGED
@@ -1,41 +1,320 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- var __importDefault = (this && this.__importDefault) || function (mod) {
17
- return (mod && mod.__esModule) ? mod : { "default": mod };
18
- };
19
- Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.checkIfWebP = exports.getSpecificRendition = exports.getGifWidth = exports.getGifHeight = exports.getBestVideo = exports.getBestRenditionUrl = exports.getBestRendition = exports.getAltText = exports.getPingbackId = exports.getClientRect = exports.setRenditionScaleUpMaxPixels = exports.bestfit = void 0;
21
- var bestfit_1 = require("./bestfit");
22
- Object.defineProperty(exports, "bestfit", { enumerable: true, get: function () { return __importDefault(bestfit_1).default; } });
23
- Object.defineProperty(exports, "setRenditionScaleUpMaxPixels", { enumerable: true, get: function () { return bestfit_1.setRenditionScaleUpMaxPixels; } });
24
- __exportStar(require("./collections"), exports);
25
- var get_client_rect_from_el_1 = require("./get-client-rect-from-el");
26
- Object.defineProperty(exports, "getClientRect", { enumerable: true, get: function () { return __importDefault(get_client_rect_from_el_1).default; } });
27
- var get_pingback_id_1 = require("./get-pingback-id");
28
- Object.defineProperty(exports, "getPingbackId", { enumerable: true, get: function () { return __importDefault(get_pingback_id_1).default; } });
29
- var gif_utils_1 = require("./gif-utils");
30
- Object.defineProperty(exports, "getAltText", { enumerable: true, get: function () { return gif_utils_1.getAltText; } });
31
- Object.defineProperty(exports, "getBestRendition", { enumerable: true, get: function () { return gif_utils_1.getBestRendition; } });
32
- Object.defineProperty(exports, "getBestRenditionUrl", { enumerable: true, get: function () { return gif_utils_1.getBestRenditionUrl; } });
33
- Object.defineProperty(exports, "getBestVideo", { enumerable: true, get: function () { return gif_utils_1.getBestVideo; } });
34
- Object.defineProperty(exports, "getGifHeight", { enumerable: true, get: function () { return gif_utils_1.getGifHeight; } });
35
- Object.defineProperty(exports, "getGifWidth", { enumerable: true, get: function () { return gif_utils_1.getGifWidth; } });
36
- Object.defineProperty(exports, "getSpecificRendition", { enumerable: true, get: function () { return gif_utils_1.getSpecificRendition; } });
37
- __exportStar(require("./log"), exports);
38
- __exportStar(require("./sdk-headers"), exports);
39
- var webp_check_1 = require("./webp-check");
40
- Object.defineProperty(exports, "checkIfWebP", { enumerable: true, get: function () { return webp_check_1.checkIfWebP; } });
41
- //# sourceMappingURL=index.js.map
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+
21
+ // src/log.ts
22
+ var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
23
+ LogLevel2[LogLevel2["DEBUG"] = 0] = "DEBUG";
24
+ LogLevel2[LogLevel2["INFO"] = 1] = "INFO";
25
+ LogLevel2[LogLevel2["WARN"] = 2] = "WARN";
26
+ LogLevel2[LogLevel2["ERROR"] = 3] = "ERROR";
27
+ return LogLevel2;
28
+ })(LogLevel || {});
29
+ var Logger = {
30
+ ENABLED: typeof window !== "undefined" && typeof location !== "undefined" && location.search.indexOf("giphy-debug") !== -1,
31
+ LEVEL: 0,
32
+ PREFIX: "GiphyJS",
33
+ debug: (...msg) => {
34
+ if (Logger.ENABLED && Logger.LEVEL <= 0 /* DEBUG */) {
35
+ console.debug(Logger.PREFIX, ...msg);
36
+ }
37
+ },
38
+ info: (...msg) => {
39
+ if (Logger.ENABLED && Logger.LEVEL <= 1 /* INFO */) {
40
+ console.info(Logger.PREFIX, ...msg);
41
+ }
42
+ },
43
+ warn: (...msg) => {
44
+ if (Logger.ENABLED && Logger.LEVEL <= 2 /* WARN */) {
45
+ console.warn(Logger.PREFIX, ...msg);
46
+ }
47
+ },
48
+ error: (...msg) => {
49
+ if (Logger.ENABLED && Logger.LEVEL <= 3 /* ERROR */) {
50
+ console.error(Logger.PREFIX, ...msg);
51
+ }
52
+ }
53
+ };
54
+
55
+ // src/bestfit.ts
56
+ var closestArea = (width, height, renditions) => {
57
+ let currentBest = Infinity;
58
+ let result;
59
+ renditions.forEach((rendition) => {
60
+ const widthPercentage = rendition.width / width;
61
+ const heightPercentage = rendition.height / height;
62
+ const areaPercentage = widthPercentage * heightPercentage;
63
+ const testBest = Math.abs(1 - areaPercentage);
64
+ if (testBest < currentBest) {
65
+ currentBest = testBest;
66
+ result = rendition;
67
+ }
68
+ });
69
+ return result;
70
+ };
71
+ var SCALE_UP_MAX_PIXELS = 50;
72
+ var setRenditionScaleUpMaxPixels = (pixels) => {
73
+ Logger.debug(`@giphy/js-util set rendition selection scale up max pixels to ${pixels}`);
74
+ SCALE_UP_MAX_PIXELS = pixels;
75
+ };
76
+ function bestfit(renditions, width, height, scaleUpMaxPixels = SCALE_UP_MAX_PIXELS) {
77
+ let [largestRendition] = renditions;
78
+ const testRenditions = renditions.filter((rendition) => {
79
+ if (rendition.width * rendition.height > largestRendition.width * largestRendition.height) {
80
+ largestRendition = rendition;
81
+ }
82
+ return width - rendition.width <= scaleUpMaxPixels && height - rendition.height <= scaleUpMaxPixels;
83
+ });
84
+ if (testRenditions.length === 0) {
85
+ return largestRendition;
86
+ }
87
+ return closestArea(width, height, testRenditions);
88
+ }
89
+ var bestfit_default = bestfit;
90
+
91
+ // src/collections.ts
92
+ function mapValues(object, mapFn) {
93
+ if (Array.isArray(object)) {
94
+ throw `This map is just for objects, just use array.map for arrays`;
95
+ }
96
+ return Object.keys(object).reduce((result, key) => {
97
+ result[key] = mapFn(object[key], key);
98
+ return result;
99
+ }, {});
100
+ }
101
+ function forEach(object, mapFn) {
102
+ if (Array.isArray(object)) {
103
+ throw `This map is just for objects, just use array.forEach for arrays`;
104
+ }
105
+ return Object.keys(object).forEach((key) => {
106
+ mapFn(object[key], key);
107
+ });
108
+ }
109
+ function take(arr, count = 0) {
110
+ return arr.slice(0, count);
111
+ }
112
+ function without(arr, values) {
113
+ return arr.filter((val) => values.indexOf(val) === -1);
114
+ }
115
+ function pick(object, pick2) {
116
+ const res = {};
117
+ pick2.forEach((key) => {
118
+ if (object[key] !== void 0) {
119
+ res[key] = object[key];
120
+ }
121
+ });
122
+ return res;
123
+ }
124
+
125
+ // src/get-client-rect-from-el.ts
126
+ var getClientRect = (el) => {
127
+ let left = 0;
128
+ let top = 0;
129
+ const width = el.offsetWidth;
130
+ const height = el.offsetHeight;
131
+ do {
132
+ left += el.offsetLeft;
133
+ top += el.offsetTop;
134
+ el = el.offsetParent;
135
+ } while (el);
136
+ const result = {
137
+ left,
138
+ top,
139
+ width,
140
+ height,
141
+ right: left + width,
142
+ bottom: top + height,
143
+ x: left,
144
+ y: top
145
+ };
146
+ return __spreadProps(__spreadValues({}, result), { toJSON: () => JSON.stringify(result) });
147
+ };
148
+ var get_client_rect_from_el_default = getClientRect;
149
+
150
+ // src/get-pingback-id.ts
151
+ import { v4 as uuid } from "uuid";
152
+ var pingbackId = "";
153
+ var idLength = 16;
154
+ var noUUIDRandom = () => {
155
+ let result = "";
156
+ const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
157
+ const charactersLength = characters.length;
158
+ for (let i = 0; i < idLength; i++) {
159
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
160
+ }
161
+ return result;
162
+ };
163
+ var getPingbackId = () => {
164
+ if (!pingbackId) {
165
+ try {
166
+ pingbackId = sessionStorage.getItem("giphyPingbackId");
167
+ } catch (_) {
168
+ }
169
+ if (!pingbackId) {
170
+ const hexTime = (/* @__PURE__ */ new Date()).getTime().toString(16);
171
+ try {
172
+ pingbackId = `${hexTime}${uuid().replace(/-/g, "")}`.substring(0, idLength);
173
+ } catch (error) {
174
+ pingbackId = noUUIDRandom();
175
+ }
176
+ try {
177
+ sessionStorage.setItem("giphyPingbackId", pingbackId);
178
+ } catch (_) {
179
+ }
180
+ }
181
+ }
182
+ return pingbackId;
183
+ };
184
+ var get_pingback_id_default = getPingbackId;
185
+
186
+ // src/webp-check.ts
187
+ var SUPPORTS_WEBP = null;
188
+ var checkIfWebP = new Promise((resolve) => {
189
+ if (typeof Image === "undefined") {
190
+ resolve(false);
191
+ }
192
+ const webp = new Image();
193
+ webp.onload = () => {
194
+ SUPPORTS_WEBP = true;
195
+ resolve(SUPPORTS_WEBP);
196
+ };
197
+ webp.onerror = () => {
198
+ SUPPORTS_WEBP = false;
199
+ resolve(SUPPORTS_WEBP);
200
+ };
201
+ webp.src = "data:image/webp;base64,UklGRjoAAABXRUJQVlA4IC4AAACyAgCdASoCAAIALmk0mk0iIiIiIgBoSygABc6WWgAA/veff/0PP8bA//LwYAAA";
202
+ });
203
+
204
+ // src/gif-utils.ts
205
+ var getSpecificRendition = ({ images, is_sticker: isSticker }, renditionLabel, isStill = false, useVideo = false) => {
206
+ if (!images || !renditionLabel)
207
+ return "";
208
+ isStill = isStill && !useVideo;
209
+ const rendition = images[`${renditionLabel}${isStill ? "_still" : ""}`];
210
+ if (rendition) {
211
+ if (isSticker || isStill) {
212
+ return rendition.url;
213
+ }
214
+ const webP = SUPPORTS_WEBP && rendition.webp;
215
+ return useVideo ? rendition.mp4 : webP || rendition.url;
216
+ }
217
+ return "";
218
+ };
219
+ var getBestVideo = (video, width, height) => {
220
+ let assets = video == null ? void 0 : video.assets;
221
+ if (assets) {
222
+ assets = __spreadValues({}, assets);
223
+ delete assets.source;
224
+ const filteredAssets = Object.values(assets).sort((a, b) => a.width - b.width);
225
+ return bestfit_default(filteredAssets, width, height);
226
+ }
227
+ };
228
+ var getRenditions = (type, images, video) => type === "video" && video && video.previews && !Object.keys(images).length ? video.previews : images;
229
+ var getBestRendition = (images, gifWidth, gifHeight, scaleUpMaxPixels) => {
230
+ const checkRenditions = pick(images, [
231
+ "original",
232
+ "fixed_width",
233
+ "fixed_height",
234
+ "fixed_width_small",
235
+ "fixed_height_small"
236
+ ]);
237
+ const testImages = Object.entries(checkRenditions).map(([renditionName, val]) => __spreadValues({
238
+ renditionName
239
+ }, val));
240
+ return bestfit_default(testImages, gifWidth, gifHeight, scaleUpMaxPixels);
241
+ };
242
+ var getBestRenditionUrl = ({ images, video, type }, gifWidth, gifHeight, options = { isStill: false, useVideo: false }) => {
243
+ if (!gifWidth || !gifHeight || !images)
244
+ return "";
245
+ const { useVideo, isStill, scaleUpMaxPixels } = options;
246
+ const renditions = getRenditions(type, images, video);
247
+ const { renditionName } = getBestRendition(renditions, gifWidth, gifHeight, scaleUpMaxPixels);
248
+ const key = `${renditionName}${isStill && !useVideo ? "_still" : ""}`;
249
+ const rendition = renditions[key];
250
+ const match = useVideo ? rendition.mp4 : SUPPORTS_WEBP && rendition.webp ? rendition.webp : rendition.url;
251
+ return match || "";
252
+ };
253
+ var getGifHeight = ({ images }, gifWidth) => {
254
+ const { fixed_width } = images;
255
+ if (fixed_width) {
256
+ const { width, height } = fixed_width;
257
+ const aspectRatio = width / height;
258
+ return Math.round(gifWidth / aspectRatio);
259
+ }
260
+ return 0;
261
+ };
262
+ var getGifWidth = ({ images }, gifHeight) => {
263
+ const { fixed_width } = images;
264
+ if (fixed_width) {
265
+ const { width, height } = fixed_width;
266
+ const aspectRatio = width / height;
267
+ return Math.round(gifHeight * aspectRatio);
268
+ }
269
+ return 0;
270
+ };
271
+ var getAltText = ({ alt_text, user, tags = [], is_sticker = false, title = "" }) => {
272
+ if (alt_text) {
273
+ return alt_text;
274
+ }
275
+ if (title) {
276
+ return title;
277
+ }
278
+ const username = user && user.username || "";
279
+ const filteredTags = take(without(tags, ["transparent"]), username ? 4 : 5);
280
+ return `${username ? `${username} ` : ``}${filteredTags.join(" ")} ${is_sticker ? "Sticker" : "GIF"}`;
281
+ };
282
+
283
+ // src/sdk-headers.ts
284
+ var gl = (typeof window !== "undefined" ? window : global) || {};
285
+ gl._GIPHY_SDK_HEADERS_ = gl._GIPHY_SDK_HEADERS_ || (gl.Headers ? new gl.Headers({
286
+ "X-GIPHY-SDK-PLATFORM": "web"
287
+ }) : void 0);
288
+ var getGiphySDKRequestHeaders = () => gl._GIPHY_SDK_HEADERS_;
289
+ var appendGiphySDKRequestHeader = (key, value) => {
290
+ var _a;
291
+ return (_a = getGiphySDKRequestHeaders()) == null ? void 0 : _a.set(key, value);
292
+ };
293
+ var appendGiphySDKRequestParam = (key, value) => {
294
+ var _a;
295
+ return (_a = getGiphySDKRequestHeaders()) == null ? void 0 : _a.set(key, value);
296
+ };
297
+ export {
298
+ LogLevel,
299
+ Logger,
300
+ appendGiphySDKRequestHeader,
301
+ appendGiphySDKRequestParam,
302
+ bestfit_default as bestfit,
303
+ checkIfWebP,
304
+ forEach,
305
+ getAltText,
306
+ getBestRendition,
307
+ getBestRenditionUrl,
308
+ getBestVideo,
309
+ get_client_rect_from_el_default as getClientRect,
310
+ getGifHeight,
311
+ getGifWidth,
312
+ getGiphySDKRequestHeaders,
313
+ get_pingback_id_default as getPingbackId,
314
+ getSpecificRendition,
315
+ mapValues,
316
+ pick,
317
+ setRenditionScaleUpMaxPixels,
318
+ take,
319
+ without
320
+ };
package/package.json CHANGED
@@ -1,32 +1,32 @@
1
1
  {
2
2
  "scripts": {
3
- "clean": "rm -rf ./dist ./esm",
3
+ "lint": "run -T eslint . --ext .ts,.tsx",
4
+ "clean": "rm -rf ./dist",
4
5
  "dev": "tsc --watch",
5
- "build": "tsc && npm run build:esm",
6
- "build:esm": "tsc -p ./tsconfig-esm.json",
6
+ "build": "run -T tsup src/index.ts --format cjs,esm --dts && run -T publint",
7
7
  "prepublish": "npm run clean && npm run build",
8
8
  "test": "run -T jest --config ./jestconfig.js",
9
9
  "test:watch": "run -T jest --config ./jestconfig.js --watchAll"
10
10
  },
11
11
  "name": "@giphy/js-util",
12
- "version": "4.4.2",
13
- "main": "dist/index.js",
12
+ "version": "5.0.0",
14
13
  "description": "Shared giphy js utils",
15
14
  "homepage": "https://github.com/Giphy/giphy-js/tree/master/packages/util",
15
+ "main": "dist/index.cjs",
16
16
  "types": "dist/index.d.ts",
17
- "module": "esm/index.js",
17
+ "module": "dist/index.js",
18
+ "type": "module",
18
19
  "sideEffects": false,
19
20
  "exports": {
20
21
  ".": {
21
22
  "types": "./dist/index.d.ts",
22
- "import": "./esm/index.js",
23
- "require": "./dist/index.js"
23
+ "import": "./dist/index.js",
24
+ "require": "./dist/index.cjs"
24
25
  },
25
26
  "./package.json": "./package.json"
26
27
  },
27
28
  "files": [
28
29
  "dist/",
29
- "esm/",
30
30
  "src/**/*"
31
31
  ],
32
32
  "license": "MIT",
@@ -3,12 +3,7 @@ import { forEach, mapValues, pick, take, without } from '../collections'
3
3
  import getPingbackId from '../get-pingback-id'
4
4
  import { getAltText, getBestRenditionUrl, getGifHeight, getGifWidth, getSpecificRendition } from '../gif-utils'
5
5
 
6
- jest.mock('../webp-check')
7
-
8
6
  describe('response parsing', () => {
9
- afterEach(() => {
10
- require('../webp-check').__setWebP(false)
11
- })
12
7
  test('getGifHeight getGifWidth', () => {
13
8
  const gif = {
14
9
  images: {
@@ -73,9 +68,8 @@ describe('response parsing', () => {
73
68
  expect(getBestRenditionUrl(dummyGif as IGif, 10, 10)).toBe(fixedWidth.url)
74
69
  expect(getBestRenditionUrl(dummyGif as IGif, 10, 10, { isStill: true })).toBe(fixedWidthStill.url)
75
70
  expect(getBestRenditionUrl(dummyGif as IGif, 10, 10, { useVideo: true })).toBe(fixedWidth.mp4)
76
- require('../webp-check').__setWebP(true)
77
- expect(getBestRenditionUrl(dummyGif as IGif, 10, 10)).toBe(fixedWidth.webp)
78
- expect(getBestRenditionUrl(dummyGif as IGif, 10, 10, { isStill: true })).toBe(fixedWidthStill.webp)
71
+ expect(getBestRenditionUrl(dummyGif as IGif, 10, 10)).toBe(fixedWidth.url)
72
+ expect(getBestRenditionUrl(dummyGif as IGif, 10, 10, { isStill: true })).toBe(fixedWidthStill.url)
79
73
  })
80
74
 
81
75
  test('getSpecificRendition', () => {
@@ -104,8 +98,7 @@ describe('response parsing', () => {
104
98
  expect(getSpecificRendition(dummyGif as IGif, 'fixed_width', true)).toBe(fixedWidthStill.url)
105
99
  expect(getSpecificRendition(dummyGif as IGif, 'fixed_width', true, true)).toBe(fixedWidth.mp4)
106
100
  expect(getSpecificRendition(dummyGif as IGif, 'fixed_width_still', true, true)).toBe(fixedWidthStill.mp4)
107
- require('../webp-check').__setWebP(true)
108
- expect(getSpecificRendition(dummyGif as IGif, 'fixed_width')).toBe(fixedWidth.webp)
101
+ expect(getSpecificRendition(dummyGif as IGif, 'fixed_width')).toBe(fixedWidth.url)
109
102
  expect(getSpecificRendition(dummyGif as IGif, 'dsakfjslkj')).toBe('')
110
103
  expect(getSpecificRendition({} as IGif, 'fixed_width')).toBe('')
111
104
  })
package/dist/bestfit.d.ts DELETED
@@ -1,18 +0,0 @@
1
- import { IRendition } from '@giphy/js-types';
2
- export declare const setRenditionScaleUpMaxPixels: (pixels: number) => void;
3
- /**
4
- * Finds image rendition that best fits a given container preferring images
5
- * ##### Note: all renditions are assumed to have the same aspect ratio
6
- *
7
- * When we have a portrait target and landscape gif, we choose a higher rendition to match
8
- * the height of the portrait target, otherwise it's blurry (same applies for landscape to portrait)
9
- *
10
- * @name bestfit
11
- * @function
12
- * @param {Array.<Object>} renditions available image renditions each having a width and height property
13
- * @param {Number} width
14
- * @param {Number} height
15
- * @param {Number} scaleUpMaxPixels the maximum pixels an asset should be scaled up
16
- */
17
- declare function bestfit(renditions: Array<IRendition>, width: number, height: number, scaleUpMaxPixels?: number): IRendition;
18
- export default bestfit;
package/dist/bestfit.js DELETED
@@ -1,61 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.setRenditionScaleUpMaxPixels = void 0;
4
- var log_1 = require("./log");
5
- var closestArea = function (width, height, renditions) {
6
- var currentBest = Infinity;
7
- var result;
8
- // sort the renditions so we can avoid scaling up low resolutions
9
- renditions.forEach(function (rendition) {
10
- var widthPercentage = rendition.width / width;
11
- var heightPercentage = rendition.height / height;
12
- // a width percentage of 1 is exact, 2 is double, .5 half etc
13
- var areaPercentage = widthPercentage * heightPercentage;
14
- // img could be bigger or smaller
15
- var testBest = Math.abs(1 - areaPercentage); // the closer to 0 the better
16
- if (testBest < currentBest) {
17
- currentBest = testBest;
18
- result = rendition;
19
- }
20
- });
21
- return result;
22
- };
23
- var SCALE_UP_MAX_PIXELS = 50;
24
- var setRenditionScaleUpMaxPixels = function (pixels) {
25
- log_1.Logger.debug("@giphy/js-util set rendition selection scale up max pixels to ".concat(pixels));
26
- SCALE_UP_MAX_PIXELS = pixels;
27
- };
28
- exports.setRenditionScaleUpMaxPixels = setRenditionScaleUpMaxPixels;
29
- /**
30
- * Finds image rendition that best fits a given container preferring images
31
- * ##### Note: all renditions are assumed to have the same aspect ratio
32
- *
33
- * When we have a portrait target and landscape gif, we choose a higher rendition to match
34
- * the height of the portrait target, otherwise it's blurry (same applies for landscape to portrait)
35
- *
36
- * @name bestfit
37
- * @function
38
- * @param {Array.<Object>} renditions available image renditions each having a width and height property
39
- * @param {Number} width
40
- * @param {Number} height
41
- * @param {Number} scaleUpMaxPixels the maximum pixels an asset should be scaled up
42
- */
43
- function bestfit(renditions, width, height, scaleUpMaxPixels) {
44
- if (scaleUpMaxPixels === void 0) { scaleUpMaxPixels = SCALE_UP_MAX_PIXELS; }
45
- var largestRendition = renditions[0];
46
- // filter out renditions that are smaller than the target width and height by scaleUpMaxPixels value
47
- var testRenditions = renditions.filter(function (rendition) {
48
- if (rendition.width * rendition.height > largestRendition.width * largestRendition.height) {
49
- largestRendition = rendition;
50
- }
51
- return width - rendition.width <= scaleUpMaxPixels && height - rendition.height <= scaleUpMaxPixels;
52
- });
53
- // if all are too small, use the largest we have
54
- if (testRenditions.length === 0) {
55
- return largestRendition;
56
- }
57
- // find the closest area of the filtered renditions
58
- return closestArea(width, height, testRenditions);
59
- }
60
- exports.default = bestfit;
61
- //# sourceMappingURL=bestfit.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"bestfit.js","sourceRoot":"","sources":["../src/bestfit.ts"],"names":[],"mappings":";;;AACA,6BAA8B;AAE9B,IAAM,WAAW,GAAG,UAAC,KAAa,EAAE,MAAc,EAAE,UAAwB;IACxE,IAAI,WAAW,GAAG,QAAQ,CAAA;IAC1B,IAAI,MAAkB,CAAA;IACtB,iEAAiE;IACjE,UAAU,CAAC,OAAO,CAAC,UAAC,SAAqB;QACrC,IAAM,eAAe,GAAG,SAAS,CAAC,KAAK,GAAG,KAAK,CAAA;QAC/C,IAAM,gBAAgB,GAAG,SAAS,CAAC,MAAM,GAAG,MAAM,CAAA;QAClD,6DAA6D;QAC7D,IAAM,cAAc,GAAG,eAAe,GAAG,gBAAgB,CAAA;QACzD,iCAAiC;QACjC,IAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,cAAc,CAAC,CAAA,CAAC,6BAA6B;QAC3E,IAAI,QAAQ,GAAG,WAAW,EAAE;YACxB,WAAW,GAAG,QAAQ,CAAA;YACtB,MAAM,GAAG,SAAS,CAAA;SACrB;IACL,CAAC,CAAC,CAAA;IACF,OAAO,MAAO,CAAA;AAClB,CAAC,CAAA;AAED,IAAI,mBAAmB,GAAG,EAAE,CAAA;AACrB,IAAM,4BAA4B,GAAG,UAAC,MAAc;IACvD,YAAM,CAAC,KAAK,CAAC,wEAAiE,MAAM,CAAE,CAAC,CAAA;IACvF,mBAAmB,GAAG,MAAM,CAAA;AAChC,CAAC,CAAA;AAHY,QAAA,4BAA4B,gCAGxC;AACD;;;;;;;;;;;;;GAaG;AACH,SAAS,OAAO,CACZ,UAA6B,EAC7B,KAAa,EACb,MAAc,EACd,gBAA8C;IAA9C,iCAAA,EAAA,sCAA8C;IAEzC,IAAA,gBAAgB,GAAI,UAAU,GAAd,CAAc;IACnC,oGAAoG;IACpG,IAAM,cAAc,GAAG,UAAU,CAAC,MAAM,CAAC,UAAA,SAAS;QAC9C,IAAI,SAAS,CAAC,KAAK,GAAG,SAAS,CAAC,MAAM,GAAG,gBAAgB,CAAC,KAAK,GAAG,gBAAgB,CAAC,MAAM,EAAE;YACvF,gBAAgB,GAAG,SAAS,CAAA;SAC/B;QACD,OAAO,KAAK,GAAG,SAAS,CAAC,KAAK,IAAI,gBAAgB,IAAI,MAAM,GAAG,SAAS,CAAC,MAAM,IAAI,gBAAgB,CAAA;IACvG,CAAC,CAAC,CAAA;IACF,gDAAgD;IAChD,IAAI,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE;QAC7B,OAAO,gBAAgB,CAAA;KAC1B;IACD,mDAAmD;IACnD,OAAO,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,cAAc,CAAC,CAAA;AACrD,CAAC;AAED,kBAAe,OAAO,CAAA"}
@@ -1,5 +0,0 @@
1
- export declare function mapValues(object: any, mapFn: (val: any, key: string) => any): any;
2
- export declare function forEach(object: any, mapFn: (val: any, key: any) => void): void;
3
- export declare function take<T>(arr: T[], count?: number): T[];
4
- export declare function without<T>(arr: T[], values: T[]): T[];
5
- export declare function pick<T extends object, U extends keyof T>(object: T, pick: Array<U>): Pick<T, U>;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.pick = exports.without = exports.take = exports.forEach = exports.mapValues = void 0;
4
- function mapValues(object, mapFn) {
5
- if (Array.isArray(object)) {
6
- throw "This map is just for objects, just use array.map for arrays";
7
- }
8
- return Object.keys(object).reduce(function (result, key) {
9
- result[key] = mapFn(object[key], key);
10
- return result;
11
- }, {});
12
- }
13
- exports.mapValues = mapValues;
14
- function forEach(object, mapFn) {
15
- if (Array.isArray(object)) {
16
- throw "This map is just for objects, just use array.forEach for arrays";
17
- }
18
- return Object.keys(object).forEach(function (key) {
19
- mapFn(object[key], key);
20
- });
21
- }
22
- exports.forEach = forEach;
23
- function take(arr, count) {
24
- if (count === void 0) { count = 0; }
25
- return arr.slice(0, count);
26
- }
27
- exports.take = take;
28
- function without(arr, values) {
29
- return arr.filter(function (val) { return values.indexOf(val) === -1; });
30
- }
31
- exports.without = without;
32
- function pick(object, pick) {
33
- var res = {};
34
- pick.forEach(function (key) {
35
- if (object[key] !== undefined) {
36
- res[key] = object[key];
37
- }
38
- });
39
- return res;
40
- }
41
- exports.pick = pick;
42
- //# sourceMappingURL=collections.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"collections.js","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":";;;AAAA,SAAgB,SAAS,CAAC,MAAW,EAAE,KAAqC;IACxE,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACvB,MAAM,6DAA6D,CAAA;KACtE;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,UAAC,MAAW,EAAE,GAAW;QACvD,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAA;QACrC,OAAO,MAAM,CAAA;IACjB,CAAC,EAAE,EAAE,CAAC,CAAA;AACV,CAAC;AARD,8BAQC;AAED,SAAgB,OAAO,CAAC,MAAW,EAAE,KAAmC;IACpE,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACvB,MAAM,iEAAiE,CAAA;KAC1E;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,UAAC,GAAW;QAC3C,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAA;IAC3B,CAAC,CAAC,CAAA;AACN,CAAC;AAPD,0BAOC;AAED,SAAgB,IAAI,CAAI,GAAQ,EAAE,KAAS;IAAT,sBAAA,EAAA,SAAS;IACvC,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAA;AAC9B,CAAC;AAFD,oBAEC;AAED,SAAgB,OAAO,CAAI,GAAQ,EAAE,MAAW;IAC5C,OAAO,GAAG,CAAC,MAAM,CAAC,UAAA,GAAG,IAAI,OAAA,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAA1B,CAA0B,CAAC,CAAA;AACxD,CAAC;AAFD,0BAEC;AAED,SAAgB,IAAI,CAAsC,MAAS,EAAE,IAAc;IAC/E,IAAM,GAAG,GAAe,EAAE,CAAA;IAC1B,IAAI,CAAC,OAAO,CAAC,UAAC,GAAM;QAChB,IAAI,MAAM,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE;YAC3B,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;SACzB;IACL,CAAC,CAAC,CAAA;IACF,OAAO,GAAiB,CAAA;AAC5B,CAAC;AARD,oBAQC"}
@@ -1,6 +0,0 @@
1
- /**
2
- * @param el HTMLElement
3
- * @returns calculated properties of DOMReact
4
- */
5
- declare const getClientRect: (el: HTMLElement) => DOMRectReadOnly;
6
- export default getClientRect;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- /**
15
- * @param el HTMLElement
16
- * @returns calculated properties of DOMReact
17
- */
18
- var getClientRect = function (el) {
19
- var left = 0;
20
- var top = 0;
21
- var width = el.offsetWidth;
22
- var height = el.offsetHeight;
23
- // no layout thrash
24
- do {
25
- left += el.offsetLeft;
26
- top += el.offsetTop;
27
- el = el.offsetParent;
28
- } while (el);
29
- var result = {
30
- left: left,
31
- top: top,
32
- width: width,
33
- height: height,
34
- right: left + width,
35
- bottom: top + height,
36
- x: left,
37
- y: top,
38
- };
39
- return __assign(__assign({}, result), { toJSON: function () { return JSON.stringify(result); } });
40
- };
41
- exports.default = getClientRect;
42
- //# sourceMappingURL=get-client-rect-from-el.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-client-rect-from-el.js","sourceRoot":"","sources":["../src/get-client-rect-from-el.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA;;;GAGG;AACH,IAAM,aAAa,GAAG,UAAC,EAAe;IAClC,IAAI,IAAI,GAAG,CAAC,CAAA;IACZ,IAAI,GAAG,GAAG,CAAC,CAAA;IACX,IAAM,KAAK,GAAG,EAAE,CAAC,WAAW,CAAA;IAC5B,IAAM,MAAM,GAAG,EAAE,CAAC,YAAY,CAAA;IAC9B,mBAAmB;IACnB,GAAG;QACC,IAAI,IAAI,EAAE,CAAC,UAAU,CAAA;QACrB,GAAG,IAAI,EAAE,CAAC,SAAS,CAAA;QACnB,EAAE,GAAG,EAAE,CAAC,YAA2B,CAAA;KACtC,QAAQ,EAAE,EAAC;IACZ,IAAM,MAAM,GAAG;QACX,IAAI,MAAA;QACJ,GAAG,KAAA;QACH,KAAK,OAAA;QACL,MAAM,QAAA;QACN,KAAK,EAAE,IAAI,GAAG,KAAK;QACnB,MAAM,EAAE,GAAG,GAAG,MAAM;QACpB,CAAC,EAAE,IAAI;QACP,CAAC,EAAE,GAAG;KACT,CAAA;IACD,6BAAY,MAAM,KAAE,MAAM,EAAE,cAAM,OAAA,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAtB,CAAsB,IAAE;AAC9D,CAAC,CAAA;AAED,kBAAe,aAAa,CAAA"}
@@ -1,2 +0,0 @@
1
- declare const getPingbackId: () => string;
2
- export default getPingbackId;