Skip to content

Commit

Permalink
Lodash: Remove completely from @wordpress/shortcode package (WordPr…
Browse files Browse the repository at this point in the history
…ess#41983)

* Lodash: Remove _.extend() from shortcodes

* Lodash: Remove _.pick() from shortcodes

* Lodash: Remove _.isString() from shortcodes

* Lodash: Remove _.isEqual() from shortcodes

* Lodash: Remove _.forEach() from shortcodes

* Remove lodash dependency from shortcode package

* ESLint: Deprecate _.extend()
  • Loading branch information
tyxla authored Jun 28, 2022
1 parent 5ea47b7 commit d46858f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module.exports = {
'differenceWith',
'dropRight',
'each',
'extend',
'findIndex',
'findKey',
'findLast',
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/shortcode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"react-native": "src/index",
"dependencies": {
"@babel/runtime": "^7.16.0",
"lodash": "^4.17.21",
"memize": "^1.1.0"
},
"publishConfig": {
Expand Down
26 changes: 12 additions & 14 deletions packages/shortcode/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { extend, pick, isString, isEqual, forEach } from 'lodash';
import memize from 'memize';

/**
Expand Down Expand Up @@ -260,14 +259,10 @@ export function fromMatch( match ) {
*
* @return {WPShortcode} Shortcode instance.
*/
const shortcode = extend(
const shortcode = Object.assign(
function ( options ) {
extend(
this,
pick( options || {}, 'tag', 'attrs', 'type', 'content' )
);

const attributes = this.attrs;
const { tag, attrs: attributes, type, content } = options || {};
Object.assign( this, { tag, type, content } );

// Ensure we have a correctly formatted `attrs` object.
this.attrs = {
Expand All @@ -279,17 +274,20 @@ const shortcode = extend(
return;
}

const attributeTypes = [ 'named', 'numeric' ];

// Parse a string of attributes.
if ( isString( attributes ) ) {
if ( typeof attributes === 'string' ) {
this.attrs = attrs( attributes );
// Identify a correctly formatted `attrs` object.
} else if (
isEqual( Object.keys( attributes ), [ 'named', 'numeric' ] )
attributes.length === attributeTypes.length &&
attributeTypes.every( ( t, key ) => t === attributes[ key ] )
) {
this.attrs = attributes;
// Handle a flat object of attributes.
} else {
forEach( attributes, ( value, key ) => {
Object.entries( attributes ).forEach( ( [ key, value ] ) => {
this.set( key, value );
} );
}
Expand All @@ -304,7 +302,7 @@ const shortcode = extend(
}
);

extend( shortcode.prototype, {
Object.assign( shortcode.prototype, {
/**
* Get a shortcode attribute.
*
Expand Down Expand Up @@ -346,15 +344,15 @@ extend( shortcode.prototype, {
string() {
let text = '[' + this.tag;

forEach( this.attrs.numeric, ( value ) => {
this.attrs.numeric.forEach( ( value ) => {
if ( /\s/.test( value ) ) {
text += ' "' + value + '"';
} else {
text += ' ' + value;
}
} );

forEach( this.attrs.named, ( value, name ) => {
Object.entries( this.attrs.named ).forEach( ( [ name, value ] ) => {
text += ' ' + name + '="' + value + '"';
} );

Expand Down

0 comments on commit d46858f

Please sign in to comment.