Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lodash: Remove completely from @wordpress/shortcode package #41983

Merged
merged 7 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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