/
message.js
79 lines (64 loc) · 2.39 KB
/
message.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
/*jshint node:true */
/*global describe,it */
'use strict';
var expect = require('chai').expect,
MessageFormatter = require('../lib/message.js');
describe('message creation', function () {
it('simple string formatting', function () {
var msg, m;
msg = new MessageFormatter(null, 'My name is ${first} {last}.');
m = msg.format({
first: 'Anthony',
last: 'Pipkin'
});
expect(m).to.equal('My name is Anthony Pipkin.');
});
it ('complex object formatter', function () {
var msg, m;
msg = new MessageFormatter(null, ['Some text before ', {
type: 'plural',
valueName: 'numPeople',
offset: 1,
options: {
one: 'Some message ${ph} with ${#} value',
few: ['Optional prefix text for |few| ', {
type: 'select',
valueName: 'gender',
options: {
male: 'Text for male option with \' single quotes',
female: 'Text for female option with {}',
other: 'Text for default'
}
}, ' optional postfix text'],
other: 'Some messages for the default',
'1': ['Optional prefix text ', {
type: 'select',
valueName: 'gender',
options: {
male: 'Text for male option with \' single quotes',
female: 'Text for female option with {}',
other: 'Text for default'
}
}, ' optional postfix text'],
}
}, ' and text after']);
m = msg.format({
numPeople: 4,
ph: 'whatever',
gender: 'male'
});
expect(m).to.equal("Some text before Optional prefix text for |few| Text for male option with ' single quotes optional postfix text and text after");
});
it('Simple string formatter using a custom formatter for a token', function () {
var msg, m;
msg = new MessageFormatter(null, 'Test formatter d: ${num:d}', {
d: function (locale, val) {
return +val;
}
});
m = msg.format({
num: '010'
});
expect(m).to.equal('Test formatter d: 10');
});
});