refactor(v2): import lqip-loader, fix build on Node 13 (#2544)

This commit is contained in:
Bartosz Kaszubowski 2020-04-05 20:55:36 +02:00 committed by GitHub
parent 6a20183a68
commit 980f1041dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 344 additions and 41 deletions

View file

@ -0,0 +1,49 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const sortBy = require('lodash.sortby');
/**
* toBase64
* @description it returns a Base64 image string with required formatting
* to work on the web (<img src=".." /> or in CSS url('..'))
*
* @param extension: image file extension
* @param data: base64 string
* @returns {string}
*/
const toBase64 = (extMimeType, data) => {
return `data:${extMimeType};base64,${data.toString('base64')}`;
};
/**
* toPalette
* @description takes a color swatch object, converts it to an array & returns
* only hex color
*
* @param swatch
* @returns {{palette: Array}}
*/
const toPalette = (swatch) => {
let palette = Object.keys(swatch).reduce((result, key) => {
if (swatch[key] !== null) {
result.push({
popularity: swatch[key].getPopulation(),
hex: swatch[key].getHex(),
});
}
return result;
}, []);
palette = sortBy(palette, ['popularity']);
palette = palette.map((color) => color.hex).reverse();
return palette;
};
module.exports = {
toBase64,
toPalette,
};