-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparticles.js
162 lines (143 loc) · 3.96 KB
/
particles.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
'use strict';
function Particle(particle) {
if (!(this instanceof Particle)) {
return new Particle(particle);
}
Object.defineProperty(this, 'id', { value: particle });
}
Particle.prototype = {
toString: function () {
return this.id;
},
toSource: function () {
return 'P("' + this.id + '")';
}
};
Particle.addMod = function (name, toStringGenerator) {
Particle.prototype[name] = function (/* ... */) {
var superToString = this.toString;
var newP = Object.create(this);
var toString = toStringGenerator.apply(newP, arguments);
Object.defineProperty(newP, 'toString', { value: function (_after, _before) {
if (arguments.length === 0) {
return Particle.prototype.toString.apply(this);
}
var _ret = toString.apply(this, arguments);
return (typeof _ret !== 'undefined') ? _ret : superToString.apply(this, arguments);
}});
return newP;
};
};
function contextMatches(where, when, after, before) {
var val = {
before: after,
after: before
}[where];
return (when === (val || '') || (when === '*' && typeof val !== 'undefined'));
}
Particle.addMod('mutates', function (mutatesWhere, mutatesWhen, mutatesTo) {
return function (after, before) {
if (contextMatches(mutatesWhere, mutatesWhen, after, before)) {
return mutatesTo;
}
};
});
Particle.addMod('looses', function (loosesWhere, loosesWhen, loosesHowMuch) {
var oldToString = this.toString;
return function (after, before) {
var v;
if (contextMatches(loosesWhere, loosesWhen, after, before)) {
v = oldToString.apply(this, arguments);
return v.substr(0, v.length - loosesHowMuch);
}
};
});
Particle.addMod('asSuffix', function () {
return function (after, before) {
if (!before) return '';
};
});
Particle.addMod('asPrefix', function () {
return function (after, _before) {
if (!after) return '';
};
});
Particle.addMod('asJoiner', function () {
return function (after, before) {
if (!after || !before) return '';
};
});
Particle.addMod('hides', function (hidesWhere, hidesWhen) {
return function (after, before) {
if (contextMatches(hidesWhere, hidesWhen, after, before)) {
return '';
}
};
});
function psToSource(ps) {
return ps.map(function (p) {
return p.toSource ? p.toSource() : ('String("' + String(p) + '")');
}).join(', ');
}
function conditionalToString(p, k, ps) {
return p && p.toString(ps[k+1] && String(ps[k+1]), ps[k-1] && String(ps[k-1]));
}
function Particles(ps) {
if (arguments.length > 1) {
ps = Array.prototype.slice.call(arguments);
}
if (!(this instanceof Particles)) {
return new Particles(ps);
}
this.toString = function () {
if (typeof this._string === 'undefined') {
var f_ps = [];
var prev = null;
for (var k = 0; k < ps.length; ++k) {
var p = ps[k];
if (p && p.toString(ps[k+1] && String(ps[k+1]), prev && String(prev))) {
f_ps.push(p);
prev = p;
} else {
prev = null;
}
}
this._string = f_ps.map(conditionalToString).join('');
}
return this._string;
};
this.toSource = function () {
return 'Ps([' + psToSource(ps) + '])';
};
}
function Words(ps) {
if (arguments.length > 1) {
ps = Array.prototype.slice.call(arguments);
}
if (!(this instanceof Words)) {
return new Words(ps);
}
this.toString = function () {
if (typeof this._string === 'undefined') {
var f_ps = [];
var prev = null;
for (var k = 0; k < ps.length; ++k) {
var p = ps[k];
if (p && p.toString(ps[k+1] && String(ps[k+1]), prev && String(prev))) {
f_ps.push(p);
prev = p;
} else {
prev = null;
}
}
this._string = f_ps.map(conditionalToString).join(' ');
}
return this._string;
};
this.toSource = function () {
return 'Ws([' + psToSource(ps) + '])';
};
}
exports.Particles = Particles;
exports.Particle = Particle;
exports.Words = Words;