Skip to content

Commit fd7cc52

Browse files
authored
Merge pull request #73 from kadamwhite/remove-underscore-dependency
Remove Underscore
2 parents e86895f + 11858c1 commit fd7cc52

File tree

9 files changed

+121
-116
lines changed

9 files changed

+121
-116
lines changed

.eslintrc

Lines changed: 0 additions & 11 deletions
This file was deleted.

.eslintrc.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
es6: true,
5+
node: true
6+
},
7+
extends: 'plugin:@wordpress/eslint-plugin/recommended',
8+
rules: {
9+
'one-var': [ 'error', 'never' ],
10+
'prefer-arrow-callback': [ 'error' ]
11+
}
12+
};

lib/patch.js

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
const _ = require( 'underscore' );
2-
3-
_.str = _.str = require( 'underscore.string' );
4-
_.mixin( _.str.exports() );
1+
// Ensure both arguments are strings and that str begins with starts.
2+
function startsWith( str, starts ) {
3+
return 0 === ( '' + str ).lastIndexOf( '' + starts, 0 );
4+
}
55

66
module.exports = {
77
isAb( diff ) {
88
let ab = false;
99
try {
10-
diff = diff.split( '\n' );
11-
_.each( diff, ( line ) => {
12-
if ( _.startsWith( line, 'diff --git a/' ) ) {
10+
diff.split( '\n' ).forEach( ( line ) => {
11+
if ( startsWith( line, 'diff --git a/' ) ) {
1312
throw true;
1413
}
15-
if ( _.startsWith( line, 'Index: trunk/wp-' ) ) {
14+
if ( startsWith( line, 'Index: trunk/wp-' ) ) {
1615
throw true;
1716
}
1817
} );
@@ -51,45 +50,44 @@ module.exports = {
5150
];
5251

5352
try {
54-
diff = diff.split( '\n' );
55-
_.each( diff, ( line ) => {
53+
diff.split( '\n' ).forEach( ( line ) => {
5654
// these are often the first line
57-
if ( _.startsWith( line, 'Index: src/' ) ||
58-
_.startsWith( line, 'Index: tests/' ) ||
59-
_.startsWith( line, 'Index: tools/' ) ||
60-
_.startsWith( line, 'diff --git src' ) ||
61-
_.startsWith( line, 'diff --git test' ) ||
62-
_.startsWith( line, 'diff --git tools' ) ||
63-
_.startsWith( line, 'diff --git a/src' ) ||
64-
_.startsWith( line, 'diff --git a/test' ) ||
65-
_.startsWith( line, 'diff --git a/tools' )
55+
if ( startsWith( line, 'Index: src/' ) ||
56+
startsWith( line, 'Index: tests/' ) ||
57+
startsWith( line, 'Index: tools/' ) ||
58+
startsWith( line, 'diff --git src' ) ||
59+
startsWith( line, 'diff --git test' ) ||
60+
startsWith( line, 'diff --git tools' ) ||
61+
startsWith( line, 'diff --git a/src' ) ||
62+
startsWith( line, 'diff --git a/test' ) ||
63+
startsWith( line, 'diff --git a/tools' )
6664

6765
) {
6866
throw false;
6967
}
7068

71-
_.each( wpDashExceptions, ( exception ) => {
72-
if ( _.startsWith( line, 'Index: ' + exception ) ||
73-
_.startsWith( line, 'diff --git ' + exception ) ||
74-
_.startsWith( line, 'diff --git a/' + exception )
69+
wpDashExceptions.forEach( ( exception ) => {
70+
if ( startsWith( line, 'Index: ' + exception ) ||
71+
startsWith( line, 'diff --git ' + exception ) ||
72+
startsWith( line, 'diff --git a/' + exception )
7573
) {
7674
throw false;
7775
}
7876
} );
7977

80-
_.each( noWpDashExceptions, ( exception ) => {
81-
if ( _.startsWith( line, 'Index: ' + exception ) ||
82-
_.startsWith( line, 'diff --git ' + exception ) ||
83-
_.startsWith( line, 'diff --git a/' + exception )
78+
noWpDashExceptions.forEach( ( exception ) => {
79+
if ( startsWith( line, 'Index: ' + exception ) ||
80+
startsWith( line, 'diff --git ' + exception ) ||
81+
startsWith( line, 'diff --git a/' + exception )
8482
) {
8583
throw true;
8684
}
8785
} );
8886

89-
if ( _.startsWith( line, 'Index: wp-' ) ||
90-
_.startsWith( line, 'Index: trunk/wp-' ) ||
91-
_.startsWith( line, 'diff --git wp-' ) ||
92-
_.startsWith( line, 'diff --git a/wp-' )
87+
if ( startsWith( line, 'Index: wp-' ) ||
88+
startsWith( line, 'Index: trunk/wp-' ) ||
89+
startsWith( line, 'diff --git wp-' ) ||
90+
startsWith( line, 'diff --git a/wp-' )
9391
) {
9492
throw true;
9593
}

lib/regex.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
const _ = require( 'underscore' );
2-
3-
_.str = _.str = require( 'underscore.string' );
4-
_.mixin( _.str.exports() );
5-
61
module.exports = {
72
patchAttachments( html ) {
83
return html.match( /<dt>\s*<a\s+href="([^"]+?)(diff|patch)"\s+title="View attachment">([^<]+)/g );
94
},
5+
106
urlsFromAttachmentList( html ) {
117
return html.match( /href="([^"]+)"/ );
128
},
9+
1310
longMatches( html ) {
1411
return html.match( /<dt([\s|\S]*?)dt>/g );
1512
},
13+
1614
possiblePatches( longMatches ) {
17-
return _.compact( _.map( longMatches, ( match ) => {
18-
if ( match.match( /(patch|diff)"/ ) ) {
19-
return _.clean( _.trim( _( match ).stripTags().replace( /\n/g, ' ' ) ) );
20-
}
21-
return false;
22-
} ) );
15+
return longMatches
16+
.map( ( match ) => {
17+
if ( match.match( /(patch|diff)"/ ) ) {
18+
return match
19+
// Remove any HTML tags.
20+
.replace( /<\/?[^>]+>/g, '' )
21+
// Collapse consecutive whitespace characters into one space.
22+
.replace( /\s+/g, ' ' )
23+
.trim();
24+
}
25+
return false;
26+
} )
27+
.filter( Boolean );
2328
},
29+
2430
localFileClean( file ) {
2531
return file.replace( '?', '' ).replace( /\s/g, '' );
2632
},

package.json

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,52 @@
11
{
2-
"name": "grunt-patch-wordpress",
3-
"description": "Patch your core WordPress",
4-
"version": "1.0.0",
5-
"homepage": "https://github.com/wordpress/grunt-patch-wordpress",
6-
"author": {
7-
"name": "Aaron Jorbin",
8-
"email": "[email protected]",
9-
"url": "http://aaron.jorb.in"
10-
},
11-
"repository": {
12-
"type": "git",
13-
"url": "git://github.com/wordpress/grunt-patch-wordpress.git"
14-
},
15-
"bugs": {
16-
"url": "https://github.com/wordpress/grunt-patch-wordpress/issues"
17-
},
18-
"license": "MIT",
19-
"main": "Gruntfile.js",
20-
"engines": {
21-
"node": ">= 8.9.3"
22-
},
23-
"scripts": {
24-
"test": "jest",
25-
"test:coverage": "jest --coverage",
26-
"test:watch": "jest --watch",
27-
"lint": "eslint .",
28-
"lint:fix": "eslint . --fix"
29-
},
30-
"jest": {
31-
"testMatch": [
32-
"**/test/**/*.js"
33-
]
34-
},
35-
"devDependencies": {
36-
"@wordpress/eslint-plugin": "^2.0.0",
37-
"eslint": "^5.15.1",
38-
"jest": "^24.5.0"
39-
},
40-
"dependencies": {
41-
"grunt": "^1.0.2",
42-
"inquirer": "^5.1.0",
43-
"request": "^2.83.0",
44-
"underscore": "~1.9.0",
45-
"underscore.string": "~3.3.4",
46-
"xmlrpc": "^1.3.1"
47-
},
48-
"keywords": [
49-
"gruntplugin"
50-
]
2+
"name": "grunt-patch-wordpress",
3+
"description": "Patch your core WordPress",
4+
"version": "1.0.0",
5+
"homepage": "https://github.com/wordpress/grunt-patch-wordpress",
6+
"author": {
7+
"name": "Aaron Jorbin",
8+
"email": "[email protected]",
9+
"url": "http://aaron.jorb.in"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git://github.com/wordpress/grunt-patch-wordpress.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/wordpress/grunt-patch-wordpress/issues"
17+
},
18+
"license": "MIT",
19+
"main": "Gruntfile.js",
20+
"engines": {
21+
"node": ">= 8.9.3"
22+
},
23+
"scripts": {
24+
"test": "jest",
25+
"test:coverage": "jest --coverage",
26+
"test:watch": "jest --watch",
27+
"lint": "eslint .",
28+
"lint:fix": "eslint . --fix"
29+
},
30+
"jest": {
31+
"testMatch": [
32+
"**/test/**/*.js"
33+
],
34+
"testPathIgnorePatterns": [
35+
".eslintrc.js"
36+
]
37+
},
38+
"devDependencies": {
39+
"@wordpress/eslint-plugin": "^2.0.0",
40+
"eslint": "^5.15.1",
41+
"jest": "^24.5.0"
42+
},
43+
"dependencies": {
44+
"grunt": "^1.0.2",
45+
"inquirer": "^5.1.0",
46+
"request": "^2.83.0",
47+
"xmlrpc": "^1.3.1"
48+
},
49+
"keywords": [
50+
"gruntplugin"
51+
]
5152
}

tasks/patch_wordpress.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@ const spawn = require( 'child_process' ).spawn;
1414
const inquirer = require( 'inquirer' );
1515
const url = require( 'url' );
1616
const fs = require( 'fs' );
17-
const _ = require( 'underscore' );
1817
const trac = require( '../lib/trac.js' );
1918
const patch = require( '../lib/patch.js' );
2019
const regex = require( '../lib/regex.js' );
2120
const xmlrpc = require( 'xmlrpc' );
2221
const mapOldToNewFilePath = require( '../lib/map_old_to_new_file_path.js' );
2322

24-
_.str = _.str = require( 'underscore.string' );
25-
_.mixin( _.str.exports() );
26-
2723
module.exports = function( grunt ) {
2824
let tempFile = 'wppatch.diff';
2925
const defaults = {
@@ -171,7 +167,7 @@ module.exports = function( grunt ) {
171167
] ).then( ( answers ) => {
172168
grunt.log.debug( 'answers:' + JSON.stringify( answers ) );
173169
matchUrl = options.tracUrl +
174-
regex.urlsFromAttachmentList( matches[ _.indexOf( possiblePatches, answers.patch_name ) ] )[ 1 ];
170+
regex.urlsFromAttachmentList( matches[ possiblePatches.indexOf( answers.patch_name ) ] )[ 1 ];
175171
getPatch( trac.convertToRaw( url.parse( 'https://' + matchUrl ) ), options );
176172
} );
177173
}
@@ -233,9 +229,9 @@ module.exports = function( grunt ) {
233229

234230
function localFile( error, result, code, done, options ) {
235231
if ( ! error ) {
236-
const files = _.filter( result.split( '\n' ), ( file ) => {
237-
return ( _.str.include( file, 'patch' ) || _.str.include( file, 'diff' ) );
238-
} );
232+
const files = result.split( '\n' ).filter( ( file ) => (
233+
file.includes( 'patch' ) || file.includes( 'diff' )
234+
) );
239235
grunt.log.debug( 'files: ' + JSON.stringify( files ) );
240236

241237
if ( 0 === files.length ) {

test/.eslintrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/.eslintrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
env: {
3+
jest: true,
4+
},
5+
};

test/regex.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ describe( 'regular expressions', () => {
1212

1313
expect( matches.length ).toBe( 2 );
1414
expect( longMatches.length ).toBe( 4 );
15-
expect( possiblePatches.length ).toBe( 2 );
15+
expect( possiblePatches ).toEqual( [
16+
'edit-form-comment.diff​ (626 bytes) - added by Thaicloud 7 weeks ago.',
17+
'23988-edit-comment.diff​ (1.1 KB) - added by seanchayes 13 days ago.',
18+
] );
1619
} );
1720

1821
it( 'one patch on a ticket', () => {

0 commit comments

Comments
 (0)