-
-
Notifications
You must be signed in to change notification settings - Fork 538
/
filter.js
executable file
·598 lines (579 loc) · 17 KB
/
filter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
'use strict';
/**
* Tests for 'filter' module.
*/
const expect = require('chai').expect;
const MetadataFilter = require('../../src/core/content/filter');
/**
* Test data is an array of objects. Each object must contain
* three fields: 'description', 'source' and 'expected'.
*
* 'description' is a test description used by 'it' function.
* 'source' is an function argument that used to test function.
* 'expected' is an expected value of function result.
*/
const FILTER_NULL_DATA = [{
description: 'should not call filter function for null source',
source: null,
expected: null
}, {
description: 'should not call filter function for empty source',
source: '',
expected: ''
}];
/**
* Test data for testing default filter.
* @type {Array}
*/
const DEFAULT_TEST_DATA = [{
description: 'should do nothing with clean string',
source: 'Track Title',
expected: 'Track Title'
}, {
description: 'should trim whitespaces',
source: ' Track Metafield ',
expected: 'Track Metafield'
}, {
description: 'should trim trailing whitespaces',
source: 'Track Metafield ',
expected: 'Track Metafield'
}, {
description: 'should trim leading whitespaces',
source: ' Track Metafield',
expected: 'Track Metafield'
}, {
description: 'should remove NBSPs',
source: 'Track\u00a0Metafield',
expected: 'Track\u0020Metafield'
}];
/**
* Test data for testing Youtube filter.
* @type {Array}
*/
const YOUTUBE_TEST_DATA = [{
description: 'should do nothing with clean string',
source: 'Track Title',
expected: 'Track Title'
}, {
description: 'should trim whitespaces',
source: ' Track Title ',
expected: 'Track Title'
}, {
description: 'should trim leading whitespaces',
source: ' Track Title',
expected: 'Track Title'
}, {
description: 'should trim trailing whitespaces',
source: 'Track Title ',
expected: 'Track Title'
}, {
description: 'should remove leftovers after e.g. (official video)',
source: 'Track Title ( )',
expected: 'Track Title'
}, {
description: 'should remove empty leftovers after e.g. (official video)',
source: 'Track Title ()',
expected: 'Track Title'
}, {
description: 'should remove "HD" string',
source: 'Track Title HD',
expected: 'Track Title'
}, {
description: 'should remove "HQ" string',
source: 'Track Title HQ',
expected: 'Track Title'
}, {
description: 'should extract title from single quotes',
source: '\'Track Title\'',
expected: 'Track Title'
}, {
description: 'should extract title from double quotes',
source: '"Track Title" whatever',
expected: 'Track Title'
}, {
description: 'should remove .avi extension',
source: 'Track Title.avi',
expected: 'Track Title'
}, {
description: 'should remove .wmv extension',
source: 'Track Title.wmv',
expected: 'Track Title'
}, {
description: 'should remove .mpg extension',
source: 'Track Title.mpg',
expected: 'Track Title'
}, {
description: 'should remove .flv extension',
source: 'Track Title.flv',
expected: 'Track Title'
}, {
description: 'should remove .mpeg extension',
source: 'Track Title.mpeg',
expected: 'Track Title'
}, {
description: 'should remove "**NEW**" string',
source: 'Track Title **NEW**',
expected: 'Track Title'
}, {
description: 'should remove "[whatever]" string',
source: 'Track Title [Official Video]',
expected: 'Track Title'
}, {
description: 'should remove "Video" string',
source: 'Track Title Video',
expected: 'Track Title'
}, {
description: 'should remove "Music Video" string',
source: 'Track Title Music Video',
expected: 'Track Title'
}, {
description: 'should remove "Official Video" string',
source: 'Track Title Official Video',
expected: 'Track Title'
}, {
description: 'should remove "Official Music Video" string',
source: 'Track Title Official Music Video',
expected: 'Track Title'
}, {
description: 'should remove "Audio" string',
source: 'Track Title Audio',
expected: 'Track Title'
}, {
description: 'should remove "Music Audio" string',
source: 'Track Title Music Audio',
expected: 'Track Title'
}, {
description: 'should remove "Official Audio" string',
source: 'Track Title Official Audio',
expected: 'Track Title'
}, {
description: 'should remove "Official Music Audio" string',
source: 'Track Title Official Music Audio',
expected: 'Track Title'
}, {
description: 'should remove "(official)" string',
source: 'Track Title (Official)',
expected: 'Track Title'
}, {
description: 'should remove "(oficial)" string',
source: 'Track Title (Oficial)',
expected: 'Track Title'
}, {
description: 'should remove "offizielles Video" string',
source: 'Track Title offizielles Video',
expected: 'Track Title'
}, {
description: 'should remove "video clip officiel" string',
source: 'Track Title video clip officiel',
expected: 'Track Title'
}, {
description: 'should remove "video clip" string',
source: 'Track Title video clip',
expected: 'Track Title'
}, {
description: 'should remove "vid\u00E9o clip" string',
source: 'Track Title vid\u00E9o clip',
expected: 'Track Title'
}, {
description: 'should remove "(YYYY)" string',
source: 'Track Title (2348)',
expected: 'Track Title'
}, {
description: 'should remove "(Whatever version)" string',
source: 'Track Title (Super Cool Version)',
expected: 'Track Title'
}, {
description: 'should remove "(Lyric Video)" string',
source: 'Track Title (Lyric Video)',
expected: 'Track Title'
}, {
description: 'should remove "(Whatever Lyric Video)" string',
source: 'Track Title (Official Lyric Video)',
expected: 'Track Title'
}, {
description: 'should remove "(Lyrics Video)" string',
source: 'Track Title (Lyrics Video)',
expected: 'Track Title'
}, {
description: 'should remove "(Whatever Lyrics Video)" string',
source: 'Track Title (OFFICIAL LYRICS VIDEO)',
expected: 'Track Title'
}, {
description: 'should remove "(With Lyrics)" string',
source: 'Track Title (With Lyrics)',
expected: 'Track Title'
}, {
description: 'should remove "(Official Track Stream)" string',
source: 'Track Title (Official Track Stream)',
expected: 'Track Title'
}, {
description: 'should remove live suffix',
source: 'Track Title Live',
expected: 'Track Title'
}, {
description: 'should remove (live) suffix',
source: 'Track Title (Live)',
expected: 'Track Title'
}, {
description: 'should remove (live ...) suffix',
source: 'Track Title (Live at somewhere)',
expected: 'Track Title'
}, {
description: 'should remove Full Album suffix',
source: 'Track Title Full Album',
expected: 'Track Title'
}, {
description: 'should remove "| something" suffix',
source: 'Track Title | Foo | Bar',
expected: 'Track Title'
}, {
description: 'should remove leading colon',
source: ':Track Title',
expected: 'Track Title'
}, {
description: 'should remove leading semicolon',
source: ';Track Title',
expected: 'Track Title'
}, {
description: 'should remove leading dash',
source: '-Track Title',
expected: 'Track Title'
}, {
description: 'should remove leading double quote',
source: '"Track Title',
expected: 'Track Title'
}, {
description: 'should remove trailing colon',
source: 'Track Title:',
expected: 'Track Title'
}, {
description: 'should remove trailing semicolon',
source: 'Track Title;',
expected: 'Track Title'
}, {
description: 'should remove trailing dash',
source: 'Track Title-',
expected: 'Track Title'
}, {
description: 'should remove trailing double quote',
source: 'Track Title"',
expected: 'Track Title'
}, {
description: 'should leave single quotes around joined',
source: 'Track \'n\' Title',
expected: 'Track \'n\' Title'
}, {
description: 'should remove "(whatever 2/12/18)" string',
source: 'Track Title (whatever 2/12/18)',
expected: 'Track Title'
}];
/**
* Test data for testing Remastered filter.
* @type {Array}
*/
const REMASTERED_TEST_DATA = [{
description: 'should do nothing with clean string',
source: 'Track Title',
expected: 'Track Title'
}, {
description: 'should remove "- Remastered" string',
source: 'Track Title - Remastered',
expected: 'Track Title '
}, {
description: 'should remove "(YYYY - Remaster)" string',
source: 'Track Title (2011 - Remaster)',
expected: 'Track Title '
}, {
description: 'should remove "- Remastered YYYY" string',
source: 'Track Title - Remastered 2015',
expected: 'Track Title '
}, {
description: 'should remove "(Remastered YYYY)" string',
source: 'Track Title (Remastered 2009)',
expected: 'Track Title '
}, {
description: 'should remove "[YYYY - Remaster]" string',
source: 'Track Title [2011 - Remaster]',
expected: 'Track Title '
}, {
description: 'should remove "- YYYY - Remaster" string',
source: 'Track Title - 2011 - Remaster',
expected: 'Track Title '
}, {
description: 'should remove "(Live / Remastered)" string',
source: 'Track Title (Live / Remastered)',
expected: 'Track Title '
}, {
description: 'should remove "- Live / Remastered" string',
source: 'Track Title - Live / Remastered',
expected: 'Track Title '
}, {
description: 'should remove "- YYYY Digital Remaster" string',
source: 'Track Title - 2001 Digital Remaster',
expected: 'Track Title '
}, {
description: 'should remove "- YYYY Remastered Version" string',
source: 'Track Title - 2011 Remastered Version',
expected: 'Track Title '
}, {
description: 'should remove "(Remastered)" string',
source: 'Track Title (Remastered)',
expected: 'Track Title '
}, {
description: 'should remove "[Remastered]" string',
source: 'Track Title [Remastered]',
expected: 'Track Title '
}];
/**
* Test data for testing 'MetadataFilter.decodeHtmlEntities' function.
* @type {Array}
*/
const DECODE_HTML_ENTITIES_TEST_DATA = [{
description: 'should do nothing with clean string',
source: 'Can\'t Kill Us',
expected: 'Can\'t Kill Us'
}, {
description: 'should decode HTML entity',
source: 'Can't Kill Us',
expected: 'Can\'t Kill Us'
}, {
description: 'should decode HTML entity',
source: 'Can`t Kill Us',
expected: 'Can`t Kill Us'
}, {
description: 'should decode ampersand symbol',
source: 'Artist 1 & Artist 2',
expected: 'Artist 1 & Artist 2'
}, {
description: 'should decode all HTML entities in string',
source: 'Artist`s 1 && Artist`s 2',
expected: 'Artist`s 1 && Artist`s 2'
}, {
description: 'should not decode invalid HTML entity',
source: 'Artist 1 &#xzz; Artist 2',
expected: 'Artist 1 &#xzz; Artist 2'
}];
/**
* Test data for testing extended filter.
* @type {Array}
*/
const EXTENDED_FILTER_TEST_DATA = [{
description: 'should do nothing with clean string',
source: 'Can\'t Kill Us',
expected: 'Can\'t Kill Us'
}, {
description: 'should decode HTML entity and trim string',
source: 'Can't Kill Us ',
expected: 'Can\'t Kill Us'
}];
/**
* Test data for testing 'MetadataFilter.removeZeroWidth' function.
* @type {Array}
*/
const REMOVE_ZERO_WIDTH_TEST_DATA = [{
description: 'should do nothing with clean string',
source: 'Track Metafield',
expected: 'Track Metafield'
}, {
description: 'should remove zero-width characters',
source: 'Str\u200Ding\u200B',
expected: 'String'
}, {
description: 'should remove trailing zero-width characters',
source: 'String\u200C',
expected: 'String'
}, {
description: 'should remove leading zero-width characters',
source: '\u200DString',
expected: 'String'
}];
const REMOVE_DOUBLE_TITLE_TEST_DATA = [{
description: 'should do nothing when separated strings are different',
source: 'Some data : another data',
expected: 'Some data : another data'
}, {
description: 'should do nothing when separator was not found',
source: 'Some Track title with colon: but without separator',
expected: 'Some Track title with colon: but without separator'
}, {
description: 'should remove double title',
source: 'Some track name With double title : Some track name With double title',
expected: 'Some track name With double title'
}, {
description: 'should do nothing when there are more than one separator',
source: 'this is weird : this is weird : this is weird',
expected: 'this is weird : this is weird : this is weird'
}];
const FIX_REMIX_SUFFIX_TEST_DATA = [{
description: 'should do nothing with correct suffix',
source: 'Track Title (Artist Remix)',
expected: 'Track Title (Artist Remix)'
}, {
description: 'should do nothing with correct suffix',
source: 'Track Title (Remix)',
expected: 'Track Title (Remix)'
}, {
description: 'should replace invalid suffix',
source: 'Track Title - Artist Remix',
expected: 'Track Title (Artist Remix)'
}, {
description: 'should replace invalid suffix',
source: 'Track Title - Remix',
expected: 'Track Title (Remix)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Remix',
expected: 'Track A (Remix)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Group X dub',
expected: 'Track A (Group X dub)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Group X edit',
expected: 'Track A (Group X edit)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Group X mix',
expected: 'Track A (Group X mix)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Group X Remix Edit',
expected: 'Track A (Group X Remix Edit)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - VIP',
expected: 'Track A (VIP)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Radio Edit',
expected: 'Track A (Radio Edit)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - X Radio Edit',
expected: 'Track A (X Radio Edit)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Short Version',
expected: 'Track A (Short Version)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Original Mix',
expected: 'Track A (Original Mix)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Radio Version',
expected: 'Track A (Radio Version)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Group X Radio Mix',
expected: 'Track A (Group X Radio Mix)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Continuous Mix',
expected: 'Track A (Continuous Mix)'
}, {
description: 'should replace invalid suffix',
source: 'Track A - Factoria Vocal Mix',
expected: 'Track A (Factoria Vocal Mix)'
}];
/**
* Filters data is an array of objects. Each object must contain
* four fields: 'description', 'filter', 'fields' and 'testData'.
*
* 'description' is a test description used by 'it' function.
* 'filter' is an filter instance.
* 'fields' is an array of song fields to be filtered.
* 'testData' contains test data used to test filter.
*/
const FILTERS_DATA = [{
description: 'Base filter',
filter: new MetadataFilter({ all: shouldNotBeCalled }),
fields: ['artist', 'track', 'album'],
testData: FILTER_NULL_DATA,
}, {
description: 'Trim filter',
filter: MetadataFilter.getDefaultFilter(),
fields: ['artist', 'track', 'album'],
testData: DEFAULT_TEST_DATA,
}, {
description: 'Youtube filter',
filter: MetadataFilter.getYoutubeFilter(),
fields: ['track'],
testData: YOUTUBE_TEST_DATA,
}, {
description: 'Remastered filter',
filter: MetadataFilter.getRemasteredFilter(),
fields: ['track', 'album'],
testData: REMASTERED_TEST_DATA,
}, {
description: 'removeZeroWidth',
filter: new MetadataFilter({ all: MetadataFilter.removeZeroWidth }),
fields: ['artist', 'track', 'album'],
testData: REMOVE_ZERO_WIDTH_TEST_DATA,
}, {
description: 'decodeHtmlEntities',
filter: new MetadataFilter({ all: MetadataFilter.decodeHtmlEntities }),
fields: ['artist', 'track', 'album'],
testData: DECODE_HTML_ENTITIES_TEST_DATA,
}, {
description: 'extended filter',
filter: new MetadataFilter({
all: MetadataFilter.decodeHtmlEntities
}).extend(MetadataFilter.getDefaultFilter()),
fields: ['artist', 'track', 'album'],
testData: EXTENDED_FILTER_TEST_DATA,
}, {
description: 'removeDoubleTitle',
filter: new MetadataFilter({ track: MetadataFilter.removeDoubleTitle }),
fields: ['track'],
testData: REMOVE_DOUBLE_TITLE_TEST_DATA,
}, {
description: 'fixTrackSuffix',
filter: new MetadataFilter({ track: MetadataFilter.fixTrackSuffix }),
fields: ['track'],
testData: FIX_REMIX_SUFFIX_TEST_DATA,
}];
/**
* Test filter object.
* @param {Object} filter MetadataFilter instance
* @param {Array} fields Array of fields to be filtered
* @param {Array} testData Array of test data
*/
function testFilter(filter, fields, testData) {
let filterFunctions = {
artist: filter.filterArtist.bind(filter),
track: filter.filterTrack.bind(filter),
album: filter.filterAlbum.bind(filter)
};
for (let field of fields) {
let filterFunction = filterFunctions[field];
describe(`${field} field`, function() {
for (let data of testData) {
let { description, source, expected } = data;
it(description, function() {
expect(expected).to.be.equal(filterFunction(source));
});
}
});
}
}
/**
* Function that should not be called.
* @throws {Error} if is called
*/
function shouldNotBeCalled() {
throw new Error('This function should not be called');
}
/**
* Run all tests.
*/
function runTests() {
for (let data of FILTERS_DATA) {
let { description, filter, fields, testData } = data;
describe(description, function() {
testFilter(filter, fields, testData);
});
}
}
module.exports = runTests;