-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metadata.js
171 lines (133 loc) · 4.45 KB
/
Metadata.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
import Find from 'lodash-es/find';
import ForEachRight from 'lodash-es/forEachRight';
import Get from 'lodash-es/get';
import IsNil from 'lodash-es/isNil';
import IsString from 'lodash-es/isString';
import Map from 'lodash-es/map';
import Remove from 'lodash-es/remove';
import Slugify from 'slugify';
import Uniq from 'lodash-es/uniq';
import UniqBy from 'lodash-es/uniqBy';
import {getRegexMatches} from './Regex';
const SlugRemoveRegex = /[\>\<]/g;
const SlugSpaceRegex = /(\s[~+_,\-\\\/!;:@]+\s)|((^|\s)[~+_,\-\\\/!;:@]+)|([~+_,\-\\\/!;:@]+($|\s))|([;:\|]+)/g;
const TitleFeaturedRegex = /\s(f(?:ea)?t)\.?.*/gi;
const TitleTagsRegex = /\s?(\(.*?\)|\[.*?\])/gi;
const TitleCreditsRegex = (
/\s?(?:\((?:with|f(?:ea)?t\.?)\s(.*?)\)|\[(?:with|f(?:ea)?t\.?)\s(.*?)\]|f(?:ea)?t\.?\s(.*))/gi
);
export function cleanTitle(value) {
if(IsNil(value)) {
return value;
}
return value
.replace(/&/g, 'and') // Replace ampersand with "and"
.replace(/pt\./gi, 'part') // Replace "pt." with "part"
.replace(/ft\./gi, 'featuring') // Replace "pt." with "part"
.replace(/[^\w\s]/gi, '') // Remove special characters
.replace(/\s+/g, ' ') // Remove extra spaces
.toLowerCase()
.trim();
}
export function createArtistTitle(artists) {
if(IsNil(artists) || IsString(artists)) {
return artists || null;
}
if(!Array.isArray(artists) || artists.length < 1) {
return null;
}
if(artists.length === 1) {
return artists[0];
}
// Join artists (e.g. 1, 2 & 3)
return [artists.splice(0, artists.length - 1).join(', '), ...artists].join(' & ');
}
export function createSlug(value) {
if(IsNil(value)) {
return null;
}
// Remove characters
value = value.replace(SlugRemoveRegex, '');
// Replace leading and trailing special characters
value = value.replace(SlugSpaceRegex, ' ');
// Create slug
return Slugify(value, {
lower: true,
remove: /[\[\]()'"*.?]/g
});
}
export function encodeTitle(value) {
if(IsNil(value)) {
return value;
}
return encodeURIComponent(value).replace(/%20/g, '+');
}
export function resolveArtists(title, artists) {
// Remove invalid artists
artists = Remove(artists, (artist) => !IsNil(artist));
// Remove duplicate artists
artists = UniqBy(artists, 'id');
// Ensure multiple artists exist
if(artists.length <= 1) {
return artists;
}
// Remove similar artists
Remove(artists, ({ id, title }) => {
title = cleanTitle(title);
// Find similar artist
return !IsNil(Find(artists, (artist) => {
if(artist.id === id) {
return false;
}
// Retrieve multiple artists from `artist.title`
let artists = Map(
artist.title.split(/\s[,&]\s/),
cleanTitle
);
// Check for matching artist
return artists.indexOf(title) >= 0;
}));
});
// Retrieve title credits
let credits = cleanTitle(getRegexMatches(TitleCreditsRegex, title).join(' '));
if(credits.length < 1) {
return artists;
}
// Resolve artists with no credit in `title`
return Remove(artists, (artist) =>
credits.indexOf(cleanTitle(artist.title)) < 0
);
}
export function resolveTitle(title) {
let titles = [];
// Add title
titles.push(cleanTitle(title));
// Add title without credits
titles.push(cleanTitle(title = title.replace(TitleCreditsRegex, '')));
// Add title without tags
titles.push(cleanTitle(title = title.replace(TitleTagsRegex, '')));
// Add title without featured suffix
titles.push(cleanTitle(title = title.replace(TitleFeaturedRegex, '')));
// Add title fragments (reversed)
ForEachRight(title.split(/\s[-]\s/), (fragment) => {
titles.push(cleanTitle(fragment));
});
// Remove duplicates
return Uniq(titles);
}
export function matchTitle(a, b) {
if(!IsString(a) || !IsString(b)) {
return false;
}
// Resolve titles for `a`
a = resolveTitle(a);
// Find matching title
return !IsNil(Find(resolveTitle(b), (title) =>
a.indexOf(title) >= 0
));
}
export function matchItemByTitle(items, value, name = 'title') {
return Find(items, (item) =>
matchTitle(Get(item, name), value)
);
}