Skip to content

Commit

Permalink
Remove lodash dependencies (oblador#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
oblador authored Feb 13, 2022
1 parent 84d446b commit 74a2d83
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 70 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
4 changes: 2 additions & 2 deletions bin/generate-icon.js
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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')
Expand Down
16 changes: 8 additions & 8 deletions bin/generate-material-icons.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#!/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(
'Usage: $0 [options] path/to/codepoints \nFor default template please provide --componentName and --fontFamily'
)
.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')
Expand All @@ -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) {
Expand Down
10 changes: 6 additions & 4 deletions lib/generate-icon-set-from-css.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const lodashTemplate = require('lodash.template');
const fromPairs = require('lodash.frompairs');
const fs = require('fs');

function extractGlyphMapFromCss(files, selectorPattern) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
6 changes: 2 additions & 4 deletions lib/icon-button.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -121,7 +119,7 @@ export default function createIconButtonComponent(Icon) {
>
<View style={[styles.container, blockStyle, style]} {...props}>
<Icon {...iconProps} />
{isString(children) ? (
{typeof children === 'string' ? (
<Text style={[styles.text, colorStyle]}>{children}</Text>
) : (
children
Expand Down
20 changes: 20 additions & 0 deletions lib/object-utils.js
Original file line number Diff line number Diff line change
@@ -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 };
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
45 changes: 0 additions & 45 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 74a2d83

Please sign in to comment.