Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·651 lines (555 loc) · 18 KB
/
Copy pathindex.js
File metadata and controls
executable file
·651 lines (555 loc) · 18 KB
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
var util = require('util');
util.isObject = function(arg) {
return typeof arg === 'object' && arg !== null;
};
util.isString = function(arg) {
return typeof arg === 'string';
};
util.isNumber = function(arg) {
return typeof arg === 'number';
};
function Jspf(title, creator, annotation, info, location, identifier, image, date, license, attribution, link, meta, extension, track) {
this.title = title || "";
this.creator = creator || "";
this.annotation = annotation || "";
this.info = info || "";
this.location = location || "";
this.identifier = identifier || "";
this.image = image || "";
this.date = date || "";
this.license = license || "";
this.attribution = attribution || Array();
this.link = link || Array();
this.meta = meta || Array();
this.extension = extension || Array();
this.track = track || Array();
}
Jspf.prototype.setTitle = function(title) {
if (!util.isString(title)) return false;
this.title = title;
return true;
};
Jspf.prototype.getTitle = function() {
return this.title;
};
Jspf.prototype.setCreator = function(creator) {
if (!util.isString(creator)) return false;
this.creator = creator;
return true;
};
Jspf.prototype.getCreator = function() {
return this.creator;
};
Jspf.prototype.setAnnotation = function(annotation) {
if (!util.isString(annotation)) return false;
this.annotation = annotation;
return true;
};
Jspf.prototype.getAnnotation = function() {
return this.annotation;
};
Jspf.prototype.setInfo = function(info) {
if (!util.isString(info)) return false;
this.info = info;
return true;
};
Jspf.prototype.getInfo = function() {
return this.info;
};
Jspf.prototype.setLocation = function(location) {
if (!util.isString(location)) return false;
this.location = location;
return true;
};
Jspf.prototype.getLocation = function() {
return this.location;
};
Jspf.prototype.setIdentifier = function(identifier) {
if (!util.isString(identifier)) return false;
this.identifier = identifier;
return true;
};
Jspf.prototype.getIdentifier = function() {
return this.identifier;
};
Jspf.prototype.setImage = function(image) {
if (!util.isString(image)) return false;
this.image = image;
return true;
};
Jspf.prototype.getImage = function() {
return this.image;
};
Jspf.prototype.setDate = function(date) {
if (!util.isString(date) && ! util.isDate(date)) return false;
this.date = date;
return true;
};
Jspf.prototype.getDate = function() {
return this.date;
};
Jspf.prototype.setLicense = function(license) {
if (!util.isString(license)) return false;
this.license = license;
return true;
};
Jspf.prototype.getLicense = function() {
return this.license;
};
Jspf.prototype.setAttribution = function(attribution) {
if (!util.isArray(attribution)) return false;
this.attribution = attribution;
return true;
};
Jspf.prototype.getAttribution = function() {
return this.attribution;
};
Jspf.prototype.setLink = function(link) {
if (!util.isArray(link)) return false;
this.link = link;
return true;
};
Jspf.prototype.getLink = function() {
return this.link;
};
Jspf.prototype.setMeta = function(meta) {
if (!util.isArray(meta)) return false;
this.meta = meta;
return true;
};
Jspf.prototype.getMeta = function() {
return this.meta;
};
Jspf.prototype.setExtension = function(extension) {
if (!util.isArray(extension)) return false;
this.extension = extension;
return true;
};
Jspf.prototype.getExtension = function() {
return this.extension;
};
Jspf.prototype.setTrack = function(track) {
var j = new Jspf();
if (!util.isArray(track) && !j.isTrack(track)) return false;
this.track = track;
return true;
};
Jspf.prototype.getTrack = function() {
return this.track;
};
Jspf.prototype.getTrackById = function(trackId) {
return this.track[trackId];
};
Jspf.prototype.isTrack = function(track) {
if (!util.isArray(track)) return false;
var t = new Trac();
for (var i in track) {
if (!t.isTrack(track[i])) return false;
}
return true;
};
Jspf.prototype.addTrack = function(location, identifier, title, creator, annotation, info, image, album, trackNum, duration, link, meta, extension) {
this.track.push(new Track(
location,
identifier,
title,
creator,
annotation,
info,
image,
album,
trackNum,
duration,
link,
meta,
extension
));
return true;
};
Jspf.prototype.pushTrack = function(track) {
var t = new Track();
if (!t.isTrack(track)) return false;
this.track.push(track);
return true;
};
Jspf.prototype.removeTrack = function(track) {
var t = new Track();
if (!t.isTrack(track)) return false;
for (var i in this.track) {
if (this.track[i].compare(track)) this.track.splice(i, 1);
break;
}
return true;
};
Jspf.prototype.setTrackLocation = function(trackId, location) {
if (!util.isNumber(trackId) || !util.isString(location) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setLocation(location);
return true;
};
Jspf.prototype.setTrackIdentifier = function(trackId, identifier) {
if (!util.isNumber(trackId) || !util.isString(identifier) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setIdentifier(identifier);
return true;
};
Jspf.prototype.setTrackTitle = function(trackId, title) {
if (!util.isNumber(trackId) || !util.isString(title) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setTitle(title);
return true;
};
Jspf.prototype.setTrackCreator = function(trackId, creator) {
if (!util.isNumber(trackId) || !util.isString(creator) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setCreator(creator);
return true;
};
Jspf.prototype.setTrackAnnotation = function(trackId, annotation) {
if (!util.isNumber(trackId) || !util.isString(annotation) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setAnnotation(annotation);
return true;
};
Jspf.prototype.setTrackInfo = function(trackId, info) {
if (!util.isNumber(trackId) || !util.isString(info) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setInfo(info);
return true;
};
Jspf.prototype.setTrackImage = function(trackId, image) {
if (!util.isNumber(trackId) || !util.isString(image) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setImage(image);
return true;
};
Jspf.prototype.setTrackAlbum= function(trackId, album) {
if (!util.isNumber(trackId) || !util.isString(album) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setAlbum(album);
return true;
};
Jspf.prototype.setTrackTrackNum = function(trackId, trackNum) {
if (!util.isNumber(trackId) || !util.isNumber(trackNum) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setTrackNum(trackNum);
return true;
};
Jspf.prototype.setTrackDuration = function(trackId, duration) {
if (!util.isNumber(trackId) || !util.isNumber(duration) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setDuration(duration);
return true;
};
Jspf.prototype.setTrackLink = function(trackId, link) {
if (!util.isNumber(trackId) || !util.isArray(link) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setLink(link);
return true;
};
Jspf.prototype.setTrackMeta = function(trackId, meta) {
if (!util.isNumber(trackId) || !util.isArray(meta) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setMeta(meta);
return true;
};
Jspf.prototype.setTrackExtension = function(trackId, extension) {
if (!util.isNumber(trackId) || !util.isObject(extension) || trackId > this.track.length || trackId < 0) return false;
this.track[trackId].setExtension(extension);
return true;
};
Jspf.prototype.toString = function() {
return JSON.stringify(this);
};
Jspf.prototype.isJspf = function(arg) {
if (!util.isObject(arg)) return false;
return arg instanceof Jspf;
};
Jspf.prototype.isParsableTrack = function(arg) {
if (!util.isArray(arg)) return false;
var t = new Track();
for (var i in arg) {
if (!t.parsable(arg[i])) return false;
}
return true;
};
Jspf.prototype.parsable = function(arg) {
if (!util.isObject(arg)) return false;
if (
arg["title"]!=null && util.isString(arg["title"]) &&
arg["creator"]!=null && util.isString(arg["creator"]) &&
arg["annotation"]!=null && util.isString(arg["annotation"]) &&
arg["info"]!=null && util.isString(arg["info"]) &&
arg["location"]!=null && util.isString(arg["location"]) &&
arg["identifier"]!=null && util.isString(arg["identifier"]) &&
arg["image"]!=null && util.isString(arg["image"]) &&
arg["date"]!=null && (util.isString(arg["date"]) || util.isDate(arg["date"]))&&
arg["license"]!=null && util.isString(arg["license"]) &&
arg["attribution"]!=null && util.isArray(arg["attribution"]) &&
arg["link"]!=null && util.isArray(arg["link"]) &&
arg["meta"]!=null && util.isArray(arg["meta"]) &&
arg["extension"]!=null && util.isObject(arg["extension"]) &&
arg["track"]!=null && this.isParsableTrack(arg["track"])) return true;
return false;
};
/*
* @param path - path of the file in which the playlist is stored
* @param callback - callback called when finished or when an error occure. Params are boolean is_error, string msg, Error err.
*/
Jspf.prototype.saveToDisk = function(path, callback) {
var fs = require('fs');
var jspf = this;
fs.open(path, 'w', function(err, fd) {
if (err) return callback(true, "Could not open File.", err);
fs.write(fd, jspf.toString(), 0, 'utf8', function(err, written, string) {
if (err) return callback(true, "Could not write to file", err);
return callback(false, "File written successfully");
});
});
};
Jspf.prototype.loadFromDisk = function(path, callback) {
var fs = require('fs');
var jspf = this;
fs.readFile(path, function(err, data) {
if (err) return callback(true, "Could not open File.", err);
var o = JSON.parse(data);
if (!jspf.parsable(o)) return callback(true, "Object stored in file is not a JSPF playlist");
jspf.setTitle(o["title"]);
jspf.setCreator(o["creator"]);
jspf.setAnnotation(o["annotation"]);
jspf.setInfo(o["info"]);
jspf.setLocation(o["location"]);
jspf.setIdentifier(o["identifier"]);
jspf.setImage(o["image"]);
jspf.setDate(o["date"]);
jspf.setLicense(o["license"]);
jspf.setAttribution(o["attribution"]);
jspf.setLink(o["link"]);
jspf.setMeta(o["meta"]);
jspf.setExtension(o["extension"]);
jspf.setTrack(o["track"]);
return callback(false, "Playlist loaded successfully", "");
});
};
function Track(location, identifier, title, creator, annotation, info, image, album, trackNum, duration, link, meta, extension) {
this.location = location || "";
this.identifier = identifier || "";
this.title = title || "";
this.creator = creator || "";
this.annotation = annotation || "";
this.info = info || "";
this.image = image || "";
this.album = album || "";
this.trackNum = trackNum || 0;
this.duration = duration || 0;
this.link = link || Array();
this.meta = meta || Array();
this.extension = extension || {};
}
Track.prototype.setLocation = function(location) {
if (!util.isString(location)) return false;
this.location = location;
return true;
};
Track.prototype.getLocation = function() {
return this.location;
};
Track.prototype.setIdentifier = function(identifier) {
if (!util.isString(identifier)) return false;
this.identifier = identifier;
return true;
};
Track.prototype.getIdentifier = function() {
return this.identifier;
};
Track.prototype.setTitle = function(title) {
if (!util.isString(title)) return false;
this.title = title;
return true;
};
Track.prototype.getTitle = function() {
return this.title;
};
Track.prototype.setCreator = function(creator) {
if (!util.isString(creator)) return false;
this.creator = creator;
return true;
};
Track.prototype.getCreator = function() {
return this.creator;
};
Track.prototype.setAnnotation = function(annotation) {
if (!util.isString(annotation)) return false;
this.annotation = annotation;
return true;
};
Track.prototype.getAnnotation = function() {
return this.annotation;
};
Track.prototype.setInfo = function(info) {
if (!util.isString(info)) return false;
this.info = info;
return true;
};
Track.prototype.getInfo = function() {
return this.info;
};
Track.prototype.setImage = function(image) {
if (!util.isString(image)) return false;
this.image = image;
return true;
};
Track.prototype.getImage = function() {
return this.image;
};
Track.prototype.setAlbum = function(album) {
if (!util.isString(album)) return false;
this.album = album;
return true;
};
Track.prototype.getAlbum = function() {
return this.album;
};
Track.prototype.setTrackNum = function(trackNum) {
if (!util.isNumber(trackNum)) return false;
this.trackNum = trackNum;
return true;
};
Track.prototype.getTrackNum = function() {
return this.trackNum;
};
Track.prototype.setDuration = function(duration) {
if (!util.isNumber(duration)) return false;
this.duration = duration;
return true;
};
Track.prototype.getDuration = function() {
return this.duration;
};
Track.prototype.setLink = function(link) {
if (!util.isArray(link)) return false;
this.link = link;
return true;
};
Track.prototype.getLink = function() {
return this.link;
};
Track.prototype.setMeta = function(meta) {
if (!util.isArray(meta)) return false;
this.meta = meta;
return true;
};
Track.prototype.getMeta = function() {
return this.meta;
};
Track.prototype.setExtension = function(extension) {
if (!util.isObject(extension)) return false;
this.extension = extension;
return true;
};
Track.prototype.getExtension = function() {
return this.extension;
};
Track.prototype.isTrack = function(arg) {
if (!util.isObject(arg)) return false;
return arg instanceof Track;
};
Track.prototype.compare = function(track) {
if (!this.isTrack(track)) return false;
if (
this.location == track.location &&
this.identifier == track.identifier &&
this.title == track.title &&
this.creator == track.creator &&
this.annotation == track.annotation &&
this.info == track.info &&
this.image == track.image &&
this.album == track.album &&
this.trackNum == track.trackNum &&
this.duration == track.duration &&
arrayCompare(this.link, track.link) &&
arrayCompare(this.meta, track.meta) &&
objectCompare(this.extension, track.extension)) return true;
return false;
}
Track.prototype.parsable = function(arg) {
if (!util.isObject(arg)) return false;
if (
arg["location"]!=null && util.isString(arg["location"]) &&
arg["identifier"]!=null && util.isString(arg["identifier"]) &&
arg["title"]!=null && util.isString(arg["title"]) &&
arg["creator"]!=null && util.isString(arg["creator"]) &&
arg["annotation"]!=null && util.isString(arg["annotation"]) &&
arg["info"]!=null && util.isString(arg["info"]) &&
arg["image"]!=null && util.isString(arg["image"]) &&
arg["album"]!=null && util.isString(arg["album"]) &&
arg["trackNum"]!=null && util.isNumber(arg["trackNum"]) &&
arg["duration"]!=null && util.isNumber(arg["duration"]) &&
arg["link"]!=null && util.isArray(arg["link"]) &&
arg["meta"]!=null && util.isArray(arg["meta"]) &&
arg["extension"]!=null && util.isObject(arg["extension"])) return true;
return false;
};
Track.prototype.toString = function() {
return JSON.stringify(this);
};
module.exports.Track = Track;
module.exports.Jspf = Jspf;
// attach the .equals method to Array's prototype to call it on any array
function arrayCompare(array, array2) {
// if the other array is a falsy value, return
if (!array || !array2)
return false;
// compare lengths - can save a lot of time
if (array.length != array2.length)
return false;
for (var i = 0, l=array.length; i < l; i++) {
// Check if we have nested arrays
if (array[i] instanceof Array && array2[i] instanceof Array) {
// recurse into the nested arrays
if (!array[i].equals(array2[i]))
return false;
}
else if (array[i] != array2[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
};
function objectCompare(object, object2) {
//For the first loop, we only check for types
for (propName in object) {
//Check for inherited methods and properties - like .equals itself
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
//Return false if the return value is different
if (object.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
return false;
}
//Check instance type
else if (typeof object[propName] != typeof object2[propName]) {
//Different types => not equal
return false;
}
}
//Now a deeper check using other objects property names
for (propName in object2) {
//We must check instances anyway, there may be a property that only exists in object2
//I wonder, if remembering the checked values from the first loop would be faster or not
if (object.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
return false;
}
else if (typeof object[propName] != typeof object2[propName]) {
return false;
}
//If the property is inherited, do not check any more (it must be equa if both objects inherit it)
if (!object.hasOwnProperty(propName))
continue;
//Now the detail check and recursion
//This returns the script back to the array comparing
if (object[propName] instanceof Array && object2[propName] instanceof Array) {
// recurse into the nested arrays
if (!object[propName].equals(object2[propName]))
return false;
}
else if (object[propName] instanceof Object && object2[propName] instanceof Object) {
// recurse into another objects
if (!object[propName].equals(object2[propName]))
return false;
}
//Normal value comparison for strings and numbers
else if(object[propName] != object2[propName]) {
return false;
}
}
//If everything passed, let's say YES
return true;
};