Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Commit 97dae38

Browse files
committed
Merge pull request #7 from yahoo/selectordinal
Add support for `selectordinal` arguments
2 parents 203f79c + 9ceed36 commit 97dae38

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
language: node_js
22
node_js:
33
- "0.10"
4+
- "0.12"

src/parser.pegjs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ argumentElement
6767
elementFormat
6868
= simpleFormat
6969
/ pluralFormat
70+
/ selectOrdinalFormat
7071
/ selectFormat
7172

7273
simpleFormat
@@ -78,11 +79,22 @@ simpleFormat
7879
}
7980

8081
pluralFormat
81-
= 'plural' _ ',' _ offset:offset? _ options:optionalFormatPattern+ {
82+
= 'plural' _ ',' _ pluralStyle:pluralStyle {
8283
return {
83-
type : 'pluralFormat',
84-
offset : offset || 0,
85-
options: options
84+
type : pluralStyle.type,
85+
ordinal: false,
86+
offset : pluralStyle.offset || 0,
87+
options: pluralStyle.options
88+
};
89+
}
90+
91+
selectOrdinalFormat
92+
= 'selectordinal' _ ',' _ pluralStyle:pluralStyle {
93+
return {
94+
type : pluralStyle.type,
95+
ordinal: true,
96+
offset : pluralStyle.offset || 0,
97+
options: pluralStyle.options
8698
}
8799
}
88100

@@ -91,7 +103,7 @@ selectFormat
91103
return {
92104
type : 'selectFormat',
93105
options: options
94-
}
106+
};
95107
}
96108

97109
selector
@@ -112,6 +124,15 @@ offset
112124
return number;
113125
}
114126

127+
pluralStyle
128+
= offset:offset? _ options:optionalFormatPattern+ {
129+
return {
130+
type : 'pluralFormat',
131+
offset : offset,
132+
options: options
133+
};
134+
}
135+
115136
// -- Helpers ------------------------------------------------------------------
116137

117138
ws 'whitespace' = [ \t\n\r]+

test/unit/parser.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,76 @@ describe('parse()', function () {
173173
});
174174
});
175175

176+
describe('parse("{floor, selectordinal, =0{ground} one{#st} two{#nd} few{#rd} other{#th}} floor")', function () {
177+
var msg = '{floor, selectordinal, =0{ground} one{#st} two{#nd} few{#rd} other{#th}} floor';
178+
var ast = parse(msg);
179+
180+
it('should contain 2 `elements`', function () {
181+
expect(ast.elements).to.have.length(2);
182+
});
183+
184+
it('should contain an `argumentElement`', function () {
185+
var element = ast.elements[0];
186+
expect(element).to.be.an('object');
187+
expect(element).to.have.property('type');
188+
expect(element.type).to.equal('argumentElement');
189+
expect(element).to.have.property('id');
190+
expect(element.id).to.equal('floor');
191+
expect(element).to.have.property('format');
192+
193+
var format = element.format;
194+
expect(format).to.be.an('object');
195+
expect(format).to.have.property('type');
196+
expect(format.type).to.equal('pluralFormat');
197+
expect(format).to.have.property('offset');
198+
expect(format.offset).to.equal(0);
199+
expect(format.ordinal).to.equal(true);
200+
});
201+
202+
it('should contain 5 `options`', function () {
203+
var options = ast.elements[0].format.options;
204+
expect(options).to.have.length(5);
205+
206+
var option = options[0];
207+
expect(option).to.be.an('object');
208+
expect(option).to.have.property('type');
209+
expect(option.type).to.equal('optionalFormatPattern');
210+
expect(option).to.have.property('selector');
211+
expect(option.selector).to.equal('=0');
212+
expect(option).to.have.property('value');
213+
expect(option.value).to.be.an('object');
214+
215+
expect(options[1].selector).to.equal('one');
216+
expect(options[2].selector).to.equal('two');
217+
expect(options[3].selector).to.equal('few');
218+
expect(options[4].selector).to.equal('other');
219+
});
220+
221+
it('should contain nested `messageFormatPattern` values for each option', function () {
222+
var options = ast.elements[0].format.options;
223+
224+
var value = options[0].value;
225+
expect(value).to.have.property('type');
226+
expect(value.type).to.equal('messageFormatPattern');
227+
expect(value).to.have.property('elements');
228+
expect(value.elements).to.be.an('array');
229+
expect(value.elements).to.have.length(1);
230+
231+
var element = value.elements[0];
232+
expect(element).to.be.an('object');
233+
expect(element).to.have.property('type');
234+
expect(element.type).to.equal('messageTextElement');
235+
expect(element).to.have.property('value');
236+
expect(element.value).to.equal('ground');
237+
238+
expect(options[0].value.elements[0].value).to.equal('ground');
239+
expect(options[1].value.elements[0].value).to.equal('#st');
240+
expect(options[2].value.elements[0].value).to.equal('#nd');
241+
expect(options[3].value.elements[0].value).to.equal('#rd');
242+
expect(options[4].value.elements[0].value).to.equal('#th');
243+
});
244+
});
245+
176246
describe('parse("{gender, select, female {woman} male {man} other {person}}")', function () {
177247
var msg = '{gender, select, female {woman} male {man} other {person}}';
178248
var ast = parse(msg);

0 commit comments

Comments
 (0)