@giphy/js-util
Advanced tools
Comparing version
@@ -34,3 +34,5 @@ "use strict"; | ||
pick.forEach(function (key) { | ||
res[key] = object[key]; | ||
if (object[key] !== undefined) { | ||
res[key] = object[key]; | ||
} | ||
}); | ||
@@ -37,0 +39,0 @@ return res; |
@@ -1,3 +0,2 @@ | ||
import { IGif, IRendition } from '@giphy/js-types'; | ||
import { IImages } from '@giphy/js-types/dist/gif'; | ||
import { IGif, IRendition, IImages } from '@giphy/js-types'; | ||
export declare const getSpecificRendition: ({ images, is_sticker: isSticker }: IGif, renditionLabel: string, isStill?: boolean, useVideo?: boolean) => string; | ||
@@ -13,3 +12,3 @@ interface IRenditionWithName extends IRendition { | ||
}; | ||
export declare const getBestRenditionUrl: ({ images }: IGif, gifWidth: number, gifHeight: number, options?: Options) => "" | "fixed_height_still" | "original_still" | "fixed_width" | "fixed_height_small_still" | "fixed_height_downsampled" | "preview" | "fixed_height_small" | "downsized_still" | "downsized" | "downsized_large" | "fixed_width_small_still" | "preview_webp" | "fixed_width_still" | "fixed_width_small" | "downsized_small" | "fixed_width_downsampled" | "downsized_medium" | "original" | "fixed_height" | "looping" | "original_mp4" | "preview_gif" | "480w_still"; | ||
export declare const getBestRenditionUrl: ({ images, video, type }: IGif, gifWidth: number, gifHeight: number, options?: Options) => "" | "fixed_height_still" | "original_still" | "fixed_width" | "fixed_height_small_still" | "fixed_height_downsampled" | "preview" | "fixed_height_small" | "downsized_still" | "downsized" | "downsized_large" | "fixed_width_small_still" | "preview_webp" | "fixed_width_still" | "fixed_width_small" | "downsized_small" | "fixed_width_downsampled" | "downsized_medium" | "original" | "fixed_height" | "looping" | "original_mp4" | "preview_gif" | "480w_still"; | ||
export declare const getGifHeight: ({ images }: IGif, gifWidth: number) => number; | ||
@@ -16,0 +15,0 @@ export declare const getGifWidth: ({ images }: IGif, gifHeight: number) => number; |
@@ -38,2 +38,5 @@ "use strict"; | ||
}; | ||
var getRenditions = function (type, images, video) { | ||
return type === 'video' && video && video.previews ? video.previews : images; | ||
}; | ||
exports.getBestRendition = function (images, gifWidth, gifHeight, scaleUpMaxPixels) { | ||
@@ -54,3 +57,3 @@ var checkRenditions = collections_1.pick(images, [ | ||
exports.getBestRenditionUrl = function (_a, gifWidth, gifHeight, options) { | ||
var images = _a.images; | ||
var images = _a.images, video = _a.video, type = _a.type; | ||
if (options === void 0) { options = { isStill: false, useVideo: false }; } | ||
@@ -60,8 +63,9 @@ if (!gifWidth || !gifHeight || !images) | ||
var useVideo = options.useVideo, isStill = options.isStill, scaleUpMaxPixels = options.scaleUpMaxPixels; | ||
var renditionName = exports.getBestRendition(images, gifWidth, gifHeight, scaleUpMaxPixels).renditionName; | ||
var renditions = getRenditions(type, images, video); | ||
var renditionName = exports.getBestRendition(renditions, gifWidth, gifHeight, scaleUpMaxPixels).renditionName; | ||
// still, video, webp or gif | ||
var key = "" + renditionName + (isStill && !useVideo ? '_still' : ''); | ||
// @ts-ignore come back to this | ||
var rendition = images[key]; | ||
var rendition = renditions[key]; | ||
var match = useVideo ? rendition.mp4 : webp_check_1.SUPPORTS_WEBP && rendition.webp ? rendition.webp : rendition.url; | ||
return match || ''; | ||
return (match || ''); | ||
}; | ||
@@ -68,0 +72,0 @@ exports.getGifHeight = function (_a, gifWidth) { |
@@ -11,3 +11,3 @@ { | ||
"name": "@giphy/js-util", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"main": "dist/index.js", | ||
@@ -25,3 +25,3 @@ "description": "Shared giphy js utils", | ||
"dependencies": { | ||
"@giphy/js-types": "^1.2.0" | ||
"@giphy/js-types": "^2.0.0" | ||
}, | ||
@@ -31,3 +31,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "2f66705d00005eb35a91d63d6dc76b32c78b04e3" | ||
"gitHead": "296180057a9d81c5d47a325cd3eff8c4b1c69b35" | ||
} |
@@ -56,3 +56,2 @@ import { getGifHeight, getAltText, getBestRenditionUrl, getSpecificRendition, getGifWidth } from '../gif-utils' | ||
url: 'fixed_width url', | ||
size: '129', | ||
webp: 'fixed_width webp', | ||
@@ -65,3 +64,2 @@ mp4: 'fixed_width mp4', | ||
url: 'fixed_width url still', | ||
size: '129', | ||
webp: 'fixed_width_still webp', | ||
@@ -90,3 +88,2 @@ mp4: 'fixed_width mp4?', | ||
url: 'fixed_width url', | ||
size: '129', | ||
webp: 'fixed_width webp', | ||
@@ -99,3 +96,2 @@ mp4: 'fixed_width mp4', | ||
url: 'fixed_width url still', | ||
size: '129', | ||
webp: 'fixed_width_still webp', | ||
@@ -173,3 +169,6 @@ mp4: 'fixed_width mp4?', | ||
expect(res).toEqual({ one: 1 }) | ||
// @ts-ignore | ||
const unmatchedProperty = pick({ g: 123 }, ['b']) | ||
expect(unmatchedProperty).toEqual({}) | ||
}) | ||
}) |
@@ -38,2 +38,6 @@ import { getBestRenditionUrl } from '../gif-utils' | ||
}) | ||
test('getBestRenditionUrl type video ', () => { | ||
expect(getBestRenditionUrl(testGifVideo, 450, 350)).toEqual('/media/600x338.gif') | ||
expect(getBestRenditionUrl(testGifVideo, 400, 300)).toEqual('/media/600x338.gif') | ||
}) | ||
}) | ||
@@ -202,1 +206,7 @@ | ||
} | ||
const testGifVideo: any = { | ||
type: 'video', | ||
images: {}, | ||
video: { previews: testGifLandscape.images }, | ||
} |
@@ -31,5 +31,7 @@ export function mapValues(object: any, mapFn: (val: any, key: string) => any): any { | ||
pick.forEach((key: U) => { | ||
res[key] = object[key] | ||
if (object[key] !== undefined) { | ||
res[key] = object[key] | ||
} | ||
}) | ||
return res as Pick<T, U> | ||
} |
import { take, pick, without } from './collections' | ||
import bestfit from './bestfit' | ||
import { IGif, ImageAllTypes, IRendition } from '@giphy/js-types' | ||
import { IImages } from '@giphy/js-types/dist/gif' | ||
import { IGif, ImageAllTypes, IRendition, IImages } from '@giphy/js-types' | ||
import { SUPPORTS_WEBP } from './webp-check' | ||
import IVideo from '@giphy/js-types/dist/video' | ||
@@ -33,2 +33,5 @@ export const getSpecificRendition = ( | ||
const getRenditions = (type: 'video' | 'gif', images: IImages, video?: IVideo) => | ||
type === 'video' && video && video.previews ? video.previews : images | ||
export const getBestRendition = ( | ||
@@ -60,3 +63,3 @@ images: IImages, | ||
export const getBestRenditionUrl = ( | ||
{ images }: IGif, | ||
{ images, video, type }: IGif, | ||
gifWidth: number, | ||
@@ -68,8 +71,11 @@ gifHeight: number, | ||
const { useVideo, isStill, scaleUpMaxPixels } = options | ||
const { renditionName } = getBestRendition(images, gifWidth, gifHeight, scaleUpMaxPixels) | ||
const key = `${renditionName}${isStill && !useVideo ? '_still' : ''}` | ||
// @ts-ignore come back to this | ||
const rendition = images[key] | ||
const renditions = getRenditions(type, images, video) | ||
const { renditionName } = getBestRendition(renditions, gifWidth, gifHeight, scaleUpMaxPixels) | ||
// still, video, webp or gif | ||
const key = `${renditionName}${isStill && !useVideo ? '_still' : ''}` as keyof IImages | ||
const rendition = renditions[key] as ImageAllTypes | ||
const match = useVideo ? rendition.mp4 : SUPPORTS_WEBP && rendition.webp ? rendition.webp : rendition.url | ||
return match || '' | ||
return (match || '') as keyof IImages | ||
} | ||
@@ -76,0 +82,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
53007
2.55%1033
1.77%+ Added
- Removed
Updated