From 74a2d83d054dc8fac52035fa02b5de2c2c070650 Mon Sep 17 00:00:00 2001 From: Joel Arvidsson Date: Sun, 13 Feb 2022 11:23:33 +0100 Subject: [PATCH] Remove lodash dependencies (#1410) --- README.md | 2 +- bin/generate-icon.js | 4 +-- bin/generate-material-icons.js | 16 +++++------ lib/generate-icon-set-from-css.js | 10 ++++--- lib/icon-button.js | 6 ++--- lib/object-utils.js | 20 ++++++++++++++ package.json | 6 ----- yarn.lock | 45 ------------------------------- 8 files changed, 39 insertions(+), 70 deletions(-) create mode 100644 lib/object-utils.js diff --git a/README.md b/README.md index 105588a5c..6555a7733 100644 --- a/README.md +++ b/README.md @@ -668,7 +668,7 @@ CSS selector prefix [default: ".icon-"] #### `-t`, `--template` -Template in lodash format [default: "./template/iconSet.tpl"] +Template in JS template string format [default: "./template/iconSet.tpl"] For default template please provide `--componentName` and `--fontFamily`. diff --git a/bin/generate-icon.js b/bin/generate-icon.js index 4763dd41c..2c41eff8e 100755 --- a/bin/generate-icon.js +++ b/bin/generate-icon.js @@ -1,11 +1,11 @@ #!/usr/bin/env node /* eslint-disable no-console */ -const omit = require('lodash.omit'); const fs = require('fs'); const path = require('path'); const yargs = require('yargs'); const generateIconSetFromCss = require('../lib/generate-icon-set-from-css'); +const { omit } = require('../lib/object-utils'); const { argv } = yargs .usage( @@ -16,7 +16,7 @@ const { argv } = yargs .describe('p', 'CSS selector prefix') .alias('p', 'prefix') .default('t', path.resolve(__dirname, '..', 'templates/bundled-icon-set.tpl')) - .describe('t', 'Template in lodash format') + .describe('t', 'Template in JS template string format') .alias('t', 'template') .describe('o', 'Save output to file, defaults to STDOUT') .alias('o', 'output') diff --git a/bin/generate-material-icons.js b/bin/generate-material-icons.js index 9c0221fc1..02147b0b3 100755 --- a/bin/generate-material-icons.js +++ b/bin/generate-material-icons.js @@ -1,11 +1,10 @@ #!/usr/bin/env node /* eslint-disable no-console */ -const omit = require('lodash.omit'); -const lodashTemplate = require('lodash.template'); const fs = require('fs'); const path = require('path'); const yargs = require('yargs'); +const { omit } = require('../lib/object-utils'); const { argv } = yargs .usage( @@ -13,7 +12,7 @@ const { argv } = yargs ) .demand(1) .default('t', path.resolve(__dirname, '..', 'templates/bundled-icon-set.tpl')) - .describe('t', 'Template in lodash format') + .describe('t', 'Template in JS template string format') .alias('t', 'template') .describe('o', 'Save output to file, defaults to STDOUT') .alias('o', 'output') @@ -40,15 +39,16 @@ if (argv.template) { template = fs.readFileSync(argv.template, { encoding: 'utf8' }); } -let data = omit(argv, '_ $0 o output t template g glyphmap'.split(' ')); +const data = omit(argv, '_ $0 o output t template g glyphmap'.split(' ')); const glyphMap = extractGlyphMapFromCodepoints(argv._[0]); let content = JSON.stringify(glyphMap, null, ' '); if (template) { - const compiled = lodashTemplate(template); - data = data || {}; - data.glyphMap = content; - content = compiled(data); + const templateVariables = { glyphMap: content, ...data }; + content = template.replace( + /\${([^}]*)}/g, + (_, key) => templateVariables[key] + ); } if (argv.output) { diff --git a/lib/generate-icon-set-from-css.js b/lib/generate-icon-set-from-css.js index 0422ff3c7..8d447c18f 100644 --- a/lib/generate-icon-set-from-css.js +++ b/lib/generate-icon-set-from-css.js @@ -1,5 +1,3 @@ -const lodashTemplate = require('lodash.template'); -const fromPairs = require('lodash.frompairs'); const fs = require('fs'); function extractGlyphMapFromCss(files, selectorPattern) { @@ -39,7 +37,10 @@ function extractGlyphMapFromCss(files, selectorPattern) { const selectors = extractSelectorsFromRule(rule); return selectors.map(selector => [selector, glyph]); }) - .reduce((acc, glyphs) => Object.assign(acc, fromPairs(glyphs)), {}); + .reduce( + (acc, glyphs) => Object.assign(acc, Object.fromEntries(glyphs)), + {} + ); } function escapeRegExp(str) { @@ -53,7 +54,8 @@ function generateIconSetFromCss(cssFiles, selectorPrefix, template, data = {}) { ); const content = JSON.stringify(glyphMap, null, ' '); if (template) { - return lodashTemplate(template)({ glyphMap: content, ...data }); + const templateVariables = { glyphMap: content, ...data }; + return template.replace(/\${([^}]*)}/g, (_, key) => templateVariables[key]); } return content; } diff --git a/lib/icon-button.js b/lib/icon-button.js index b1a4e4017..a6e6cdf73 100644 --- a/lib/icon-button.js +++ b/lib/icon-button.js @@ -1,9 +1,7 @@ -import isString from 'lodash.isstring'; -import omit from 'lodash.omit'; -import pick from 'lodash.pick'; import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import { StyleSheet, Text, TouchableHighlight, View } from 'react-native'; +import { pick, omit } from './object-utils'; const styles = StyleSheet.create({ container: { @@ -121,7 +119,7 @@ export default function createIconButtonComponent(Icon) { > - {isString(children) ? ( + {typeof children === 'string' ? ( {children} ) : ( children diff --git a/lib/object-utils.js b/lib/object-utils.js new file mode 100644 index 000000000..d03b55b29 --- /dev/null +++ b/lib/object-utils.js @@ -0,0 +1,20 @@ +const pick = (obj, ...keys) => + keys + .flat() + .filter(key => Object.prototype.hasOwnProperty.call(obj, key)) + .reduce((acc, key) => { + acc[key] = obj[key]; + return acc; + }, {}); + +const omit = (obj, ...keysToOmit) => { + const keysToOmitSet = new Set(keysToOmit.flat()); + return Object.getOwnPropertyNames(obj) + .filter(key => !keysToOmitSet.has(key)) + .reduce((acc, key) => { + acc[key] = obj[key]; + return acc; + }, {}); +}; + +module.exports = { pick, omit }; diff --git a/package.json b/package.json index 10413c5ed..89559f17e 100644 --- a/package.json +++ b/package.json @@ -61,12 +61,6 @@ }, "license": "MIT", "dependencies": { - "lodash.frompairs": "^4.0.1", - "lodash.isequal": "^4.5.0", - "lodash.isstring": "^4.0.1", - "lodash.omit": "^4.5.0", - "lodash.pick": "^4.4.0", - "lodash.template": "^4.5.0", "prop-types": "^15.7.2", "yargs": "^16.1.1" }, diff --git a/yarn.lock b/yarn.lock index 6e235ac99..989c5b2bf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1705,51 +1705,6 @@ locate-path@^2.0.0: p-locate "^2.0.0" path-exists "^3.0.0" -lodash._reinterpolate@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d" - integrity sha1-DM8tiRZq8Ds2Y8eWU4t1rG4RTZ0= - -lodash.frompairs@^4.0.1: - version "4.0.1" - resolved "https://registry.yarnpkg.com/lodash.frompairs/-/lodash.frompairs-4.0.1.tgz#bc4e5207fa2757c136e573614e9664506b2b1bd2" - integrity sha1-vE5SB/onV8E25XNhTpZkUGsrG9I= - -lodash.isequal@^4.5.0: - version "4.5.0" - resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0" - integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA= - -lodash.isstring@^4.0.1: - version "4.0.1" - resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451" - integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE= - -lodash.omit@^4.5.0: - version "4.5.0" - resolved "https://registry.yarnpkg.com/lodash.omit/-/lodash.omit-4.5.0.tgz#6eb19ae5a1ee1dd9df0b969e66ce0b7fa30b5e60" - integrity sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA= - -lodash.pick@^4.4.0: - version "4.4.0" - resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3" - integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM= - -lodash.template@^4.5.0: - version "4.5.0" - resolved "https://registry.yarnpkg.com/lodash.template/-/lodash.template-4.5.0.tgz#f976195cf3f347d0d5f52483569fe8031ccce8ab" - integrity sha512-84vYFxIkmidUiFxidA/KjjH9pAycqW+h980j7Fuz5qxRtO9pgB7MDFTdys1N7A5mcucRiDyEq4fusljItR1T/A== - dependencies: - lodash._reinterpolate "^3.0.0" - lodash.templatesettings "^4.0.0" - -lodash.templatesettings@^4.0.0: - version "4.2.0" - resolved "https://registry.yarnpkg.com/lodash.templatesettings/-/lodash.templatesettings-4.2.0.tgz#e481310f049d3cf6d47e912ad09313b154f0fb33" - integrity sha512-stgLz+i3Aa9mZgnjr/O+v9ruKZsPsndy7qPZOchbqk2cnTU1ZaldKK+v7m54WoKIyxiuMZTKT2H81F8BeAc3ZQ== - dependencies: - lodash._reinterpolate "^3.0.0" - lodash@^4.17.13, lodash@^4.17.14: version "4.17.15" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"