-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNote.js
More file actions
executable file
·567 lines (502 loc) · 12.6 KB
/
Note.js
File metadata and controls
executable file
·567 lines (502 loc) · 12.6 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
/**
* Version 1.2.31
* Source http://github.com/breck7/note
* License MIT
*/
/**
* @param {string|array|object|Note}
*/
function Note(properties)
{
// Load from string
if (typeof properties === 'string')
return this.loadFromString(properties)
// Load from object
for (var i in properties) {
if (!properties.hasOwnProperty(i))
continue
if (typeof properties[i] === 'object')
this[i] = new Note(properties[i])
else
this[i] = properties[i]
}
}
/**
* @param {string}
* @param {int}
* @return {string}
*/
Note.strRepeat = function (string, count) {
var str = ''
for (var i = 0; i < count; i++) {
str += ' '
}
return str
}
/**
* Return a new Note with the name/value pairs that all passed notes contain.
* note: will probably be removed.
* @param {array} Array of Notes
* @return {Note}
*/
Note.union = function () {
var union = Note.unionSingle(arguments[0], arguments[1])
for (var i in arguments) {
if (i === 1) continue // skip the first one
union = Note.unionSingle(union, arguments[i])
if (!union.length()) break
}
return union
}
/**
* note: will probably be removed.
* @param {Note}
* @return {Note}
*/
Note.unionSingle = function(noteA, noteB) {
var union = new Note()
if (!(noteB instanceof Note))
return union
for (var name in noteA.names()) {
if (noteA[name] instanceof Note && noteB[name] && noteB[name] instanceof Note)
union[name] = Note.unionSingle(noteA[name], noteB[name])
if (noteA[name] === noteB[name])
union[name] = noteA[name]
}
return union
}
// Privates properties
Note.prototype.privates = []
/**
* Deletes all names and values.
* @return this
*/
Note.prototype.clear = function () {
for (var name in this.names()) {
delete this[name]
}
return this
}
/**
* Returns a deep copied note.
* @return {Note}
*/
Note.prototype.clone = function () {
return new Note(this)
}
/**
* Returns the difference between 2 notes. The difference between 2 notes is a note.
*
* b == a.patch(a.diff(b))
*
* todo: clean and refactor this.
*
* @param {Note} The note to compare the instance against.
* @return {Note}
*/
Note.prototype.diff = function (note) {
var diff = new Note()
if (!(note instanceof Note))
note = new Note(note)
for (var name in this.names()) {
// Case: Deleted
if (!(name in note)) {
diff[name] = ''
continue
}
// Different Types
if (typeof(this[name]) != typeof(note[name])) {
if (typeof note[name] === 'object')
diff[name] = new Note(note[name])
// We treat a value of 1 equal to '1'
else if (this[name] == note[name])
continue
else
diff[name] = note[name]
continue
}
// Strings, floats, etc
if (typeof(this[name]) != 'object') {
if (this[name] != note[name])
diff[name] = note[name]
continue
}
// Both are Objects
var sub_diff = this[name].diff(note[name])
if (sub_diff.length())
diff[name] = sub_diff
}
// Leftovers are Additions
for (var name in note.names()) {
if (this.isPublicProperty(name))
continue
// If name is not a public property of note2, dont copy it
if (note instanceof Note && !note.isPublicProperty(name))
continue
if (typeof note[name] != 'object') {
diff[name] = note[name]
continue
}
else if (note[name] instanceof Note)
diff[name] = new Note(note[name].toString())
else
diff[name] = new Note(note)
}
return diff
}
/**
* Search the note for a given path (xpath).
* @param {string}
* @param {note}
* @return The matching value
*/
Note.prototype.get = function (query, parent) {
if (!query)
return false
if (!parent)
parent = this
var parts = query.split(/ /g),
current = parts.shift()
// Not set
if (!(current in parent))
return false
// Leaf
if (!parts.length)
return parent[current]
// Has children
return this.get(parts.join(' '), parent[current])
}
/**
* We wrap hasOwnProperty so we can check against private properties
* @param {string}
* @return {bool}
*/
Note.prototype.isPrivateProperty = function (name) {
for (var i in this.privates) {
if (this.privates[i] === name)
return true
}
return false
}
/**
* We wrap hasOwnProperty so we can check against private properties
* @param {string}
* @return {bool}
*/
Note.prototype.isPublicProperty = function (name) {
if (!this.hasOwnProperty(name))
return false
return !this.isPrivateProperty(name)
}
/**
* Return the number of name/value pairs. Shallow.
* @return {int}
*/
Note.prototype.length = function () {
var size = 0
for (var name in this) {
if (this.isPublicProperty(name))
size++
}
return size
}
/**
* Construct the Note from a string.
* @param {string}
* @return {Note}
*/
Note.prototype.loadFromString = function (string) {
// Note always start on a name. Eliminate whitespace at beginning of string
string = string.replace(/^[\n ]*/, '')
/** Eliminate Windows \r characters and newlines at end of string.*/
string = string.replace(/\n\r/g, '\n').replace(/\r\n/g, '\n')
/** Eliminate newlines at end of string.*/
string = string.replace(/[\n ]*$/, '')
/** Note doesn't have useless lines*/
string = string.replace(/\n\n+/, '\n')
// Workaround for browsers without negative look ahead
/*
var notes_without_delimiter = string.split(/\n([^ ])/),
notes = [notes_without_delimiter[0]]
// Now we recombine notes.
for (var i = 1; i < notes_without_delimiter.length; i = i + 2) {
notes.push(notes_without_delimiter[i] + notes_without_delimiter[i+1])
}
*/
var notes = string.split(/\n(?! )/g)
for (var i in notes) {
var note = notes[i]
if (matches = note.match(/^([^ ]+)(\n|$)/)) // Note
this[matches[1]] = new Note(note.substr(matches[1].length).replace(/\n /g, '\n'))
else if (matches = note.match(/^([^ ]+) /)) // Leaf
this[matches[1]] = note.substr(matches[1].length + 1).replace(/^\n /, '').replace(/\n /g, '\n')
}
return this
}
/**
* Return all names as an object.
* @return {object}
*/
Note.prototype.names = function () {
var names = {}
for (var i in this) {
if (!this.isPublicProperty(i))
continue
names[i] = i
}
return names
}
/**
* Return the next name in the Note, given a name.
* @param {string}
* @return {string}
*/
Note.prototype.next = function (name) {
var sorted = this.toArray(),
next = sorted.indexOf(name) + 1
if (sorted[next])
return sorted[next]
return sorted[0]
}
/**
* Apply a patch to the Note instance.
* @param {Note|string}
* @return {Note}
*/
Note.prototype.patch = function (patch) {
if (typeof patch === 'string')
patch = new Note(patch)
for (var name in patch) {
// Repeat logic from names()
if (!patch.hasOwnProperty(name) || this.isPrivateProperty(name))
continue
// If patch value is a string, doesnt matter what type subject is.
if (typeof patch[name] === 'string') {
if (patch[name] === '')
delete this[name]
else
this[name] = patch[name]
continue
}
// If patch value is an int, doesnt matter what type subject is.
if (typeof patch[name] === 'number') {
this[name] = patch[name]
continue
}
// If its an empty note, delete patch.
if (patch[name] instanceof Note && !patch[name].length()) {
delete this[name]
continue
}
// If both subject value and patch value are Notes, do a recursive patch.
if (this[name] instanceof Note) {
this[name] = this[name].patch(patch[name])
continue
}
// Final case. Do a deep copy of note.
this[name] = new Note(patch[name])
}
return this
}
/**
* Return the previous name in the Note, given a name.
* @param {string}
* @return {string}
*/
Note.prototype.prev = function (name) {
var sorted = this.toSortedArray(),
next = sorted.indexOf(name) - 1
if (next >= 0)
return sorted[next]
return sorted[sorted.length - 1]
}
/**
* Recursively retrieve properties.
* @param {note}
* @return Note
*/
Note.prototype.retrieve = function (note) {
var result = new Note()
for (var name in note.names()) {
// If not doesnt have that property, continue
if (!this[name])
continue
// If the request is a leaf or empty note, set
if (!(note[name] instanceof Note) || !note[name].length()) {
result[name] = this[name]
continue
}
// Else the request is a note, make sure the subject is a note
if (!(this[name] instanceof Note))
continue
// Now time to recurse
result[name] = this[name].retrieve(note[name])
}
return result
}
/**
* Set a specific value via an xpath like query.
* @param {string}
* @param Value to set
* @return this
*/
Note.prototype.set = function (query, value) {
var parts = query.split(/ /g)
var patch = new Note()
var edge = patch
for (var i = 0; i < parts.length - 1; i++) {
edge = edge[parts[i]] = new Note()
}
edge[parts[i]] = value
this.patch(patch)
return this
}
/**
* @return {string}
*/
Note.prototype.stringify = function () {
return this.toJavascript()
}
/**
* Return an array of the keys
* @return {Array}
*/
Note.prototype.toArray = function () {
var output = []
for (var i in this) {
if (!this.hasOwnProperty(i))
continue
output.push(i)
}
return output
}
/**
* Return executable javascript code.
* @return {string}
*/
Note.prototype.toJavascript = function () {
return 'new Note(\'' + this.toString().replace(/\n/g, '\\n').replace(/\'/g, '\\\'') + '\')'
}
/**
* Return the object as JSON.
* @return {string}
*/
Note.prototype.toJSON = function () {
return JSON.stringify(this)
}
/**
* @return {object}
*/
Note.prototype.toObject = function () {
var obj = {}
for (var name in this) {
if (!this.isPublicProperty(name))
continue
if (this[name] instanceof Note)
obj[name] = this[name].toObject()
else
obj[name] = this[name]
}
return obj
}
/**
* Return a sorted array
* http://ejohn.org/blog/javascript-in-chrome/
* @return {array}
*/
Note.prototype.toSortedArray = function () {
var result = []
for (var name in this.names()) {
if (!this.isPublicProperty(name))
continue
result.push(name)
}
result = result.sort()
return result
}
/**
* Return a sorted version of the note
*
* @param {bool} Return Largest to Smallest?
* @return {note}
*/
Note.prototype.toSortedNote = function (reverse) {
var result = []
for (var name in this.names()) {
result.push(name)
}
result.sort()
if (reverse)
result.reverse()
var note = new Note()
for (var i in result) {
note[result[i]] = this[result[i]]
}
return note
}
/**
* Return a sorted version of the note by a certain property
*
* IE:
* ben
* age 29
* breck
* age 28
*
* note.toSortedNoteBy('age') (return ben, breck)
*
* @param {string}
* @param {bool} Return largest to smallest?
* @return {note}
*/
Note.prototype.toSortedNoteBy = function (property_name, reverse) {
var result = []
for (var name in this.names()) {
result.push([name, this[name][property_name]])
}
result = result.sort(function(a, b) {return (reverse ? b[1] - a[1] : a[1] - b[1])})
var note = new Note()
for (var i in result) {
note[result[i][0]] = this[result[i][0]]
}
return note
}
/**
* @return {string}
*/
Note.prototype.toString = function (spaces) {
spaces = spaces || 0
var string = ''
// Iterate over each property
for (var name in this) {
// Skip private properties
if (!this.isPublicProperty(name))
continue
// If property value is undefined
if (typeof this[name] === 'undefined') {
string += '\n'
continue
}
// Set up the name part of the name/value pair
string += Note.strRepeat(' ', spaces) + name
// If the value is a note, concatenate it
if (this[name] instanceof Note)
string += '\n' + this[name].toString(spaces + 1)
// If an object (other than class of note) snuck in there
else if (typeof this[name] === 'object')
string += '\n' + new Note(this[name]).toString(spaces + 1)
// dont put a blank string on blank values.
else if (this[name].toString() === '')
string += '\n'
// multiline string
else if (this[name].toString().match(/\n/))
string += ' \n' + Note.strRepeat(' ', spaces + 1) + this[name].toString().replace(/\n/g, '\n' + Note.strRepeat(' ', spaces + 1)) + '\n'
// Plain string
else
string += ' ' + this[name].toString() + '\n'
}
return string
}
// Export Note for use in Node.js
if (typeof exports != 'undefined')
module.exports = Note;