ultimate-base 0.3.5.0 → 0.4.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,226 +0,0 @@
1
- #= require ultimate/underscore/underscore
2
- #= require ultimate/underscore/underscore.string
3
- #= require ultimate/underscore/underscore.outcasts
4
- #= require ultimate/helpers/form_tag
5
-
6
- module "Ultimate.Helpers.FormTag"
7
-
8
- _.extend @, Ultimate.Helpers.FormTag
9
-
10
- hidden_fields = (options = {}) ->
11
- method = options['method']
12
- txt = '<div style="margin:0;padding:0;display:inline">'
13
- txt += '<input name="utf8" type="hidden" value="&#x2713;" />'
14
- if method and not (method in ['get', 'post'])
15
- txt += '<input name="_method" type="hidden" value="' + method + '" />'
16
- txt + '</div>'
17
-
18
- form_text = (action = 'http://www.example.com', options = {}) ->
19
- [remote, enctype, html_class, id, method] = _.map(_.string.words('remote enctype html_class id method'), (key) -> options[key])
20
- method = 'post' if method isnt 'get'
21
- txt = "<form accept-charset=\"UTF-8\" action=\"#{action}\""
22
- txt += " enctype=\"multipart/form-data\"" if enctype
23
- txt += " data-remote=\"true\"" if remote
24
- txt += " class=\"#{html_class}\"" if html_class
25
- txt += " id=\"#{id}\"" if id
26
- txt += " method=\"#{method}\">"
27
- txt
28
-
29
- whole_form = (action = 'javascript:;', options = {}) ->
30
- out = form_text(action, options) + hidden_fields(options)
31
- if block = _.outcasts.blockGiven(arguments)
32
- out += block() + "</form>"
33
- out
34
-
35
- test "form_tag", ->
36
- equal form_tag(), whole_form()
37
- equal form_tag({}, multipart: true), whole_form(null, enctype: true)
38
- equal form_tag({}, method: 'patch'), whole_form(null, method: 'patch')
39
- equal form_tag({}, method: 'put'), whole_form(null, method: 'put')
40
- equal form_tag({}, method: 'delete'), whole_form(null, method: 'delete')
41
- equal form_tag({}, remote: true), whole_form(null, remote: true)
42
- equal form_tag(-> 'Hello world!'), whole_form(null, -> 'Hello world!')
43
- equal form_tag(null, method: 'put', -> 'Hello world!'), whole_form(null, method: 'put', -> 'Hello world!')
44
-
45
- test "select_tag", ->
46
- equal select_tag("people", "<option>david</option>"),
47
- '<select id="people" name="people"><option>david</option></select>'
48
- equal select_tag("colors", "<option>Red</option><option>Blue</option><option>Green</option>", multiple: true),
49
- '<select id="colors" multiple="multiple" name="colors[]"><option>Red</option><option>Blue</option><option>Green</option></select>'
50
- equal select_tag("places", "<option>Home</option><option>Work</option><option>Pub</option>", disabled: true),
51
- '<select disabled="disabled" id="places" name="places"><option>Home</option><option>Work</option><option>Pub</option></select>'
52
- equal select_tag("places", "<option>Home</option><option>Work</option><option>Pub</option>", include_blank: true),
53
- '<select id="places" name="places"><option value=""></option><option>Home</option><option>Work</option><option>Pub</option></select>'
54
- equal select_tag("places", "<option>Home</option><option>Work</option><option>Pub</option>", prompt: "string"),
55
- '<select id="places" name="places"><option value="">string</option><option>Home</option><option>Work</option><option>Pub</option></select>'
56
- equal select_tag("places", "<option>Home</option><option>Work</option><option>Pub</option>", prompt: "<script>alert(1337)</script>"),
57
- '<select id="places" name="places"><option value="">&lt;script&gt;alert(1337)&lt;/script&gt;</option><option>Home</option><option>Work</option><option>Pub</option></select>'
58
- equal select_tag("places", "<option>Home</option><option>Work</option><option>Pub</option>", prompt: "string", include_blank: true),
59
- '<select id="places" name="places"><option value="">string</option><option value=""></option><option>Home</option><option>Work</option><option>Pub</option></select>'
60
- equal select_tag("places", null, include_blank: true),
61
- '<select id="places" name="places"><option value=""></option></select>'
62
- equal select_tag("places", null, prompt: "string"),
63
- '<select id="places" name="places"><option value="">string</option></select>'
64
-
65
- test "text_field_tag", ->
66
- equal text_field_tag("title"),
67
- '<input id="title" name="title" type="text" />'
68
- equal text_field_tag("title", "Hello!", class: "admin"),
69
- '<input class="admin" id="title" name="title" type="text" value="Hello!" />'
70
- equal text_field_tag("title", "Hello!", size: 75),
71
- '<input id="title" name="title" size="75" type="text" value="Hello!" />'
72
- equal text_field_tag("title", "Hello!", disabled: true),
73
- '<input disabled="disabled" id="title" name="title" type="text" value="Hello!" />'
74
- equal text_field_tag("title", "Hello!", size: 70, maxlength: 80),
75
- '<input id="title" maxlength="80" name="title" size="70" type="text" value="Hello!" />'
76
-
77
- test "label_tag", ->
78
- equal label_tag("title"), '<label for="title">Title</label>'
79
- equal label_tag("title", "My Title"), '<label for="title">My Title</label>'
80
- equal label_tag("title", "My Title", class: "small-label"), '<label class="small-label" for="title">My Title</label>'
81
- equal label_tag( -> "Blocked" ), '<label>Blocked</label>'
82
- equal label_tag( -> content_tag('span', "Blocked SPAN", class: 'inner') ), '<label><span class="inner">Blocked SPAN</span></label>'
83
- equal label_tag("clock", -> "Grandfather"), '<label for="clock">Grandfather</label>'
84
- equal label_tag("clock", id: "label_clock", -> "Grandfather"), '<label for="clock" id="label_clock">Grandfather</label>'
85
-
86
- test "hidden_field_tag", ->
87
- equal hidden_field_tag('id', 3), '<input id="id" name="id" type="hidden" value="3" />'
88
-
89
- test "file_field_tag", ->
90
- equal file_field_tag('picsplz'), '<input id="picsplz" name="picsplz" type="file" />'
91
- equal file_field_tag('picsplz', class: 'pix'), '<input class="pix" id="picsplz" name="picsplz" type="file" />'
92
-
93
- test "password_field_tag", ->
94
- equal password_field_tag(), '<input id="password" name="password" type="password" />'
95
-
96
- test "text_area_tag", ->
97
- equal text_area_tag("body", "hello world", size: "20x40"),
98
- '<textarea cols="20" id="body" name="body" rows="40">\nhello world</textarea>'
99
- equal text_area_tag("body", "hello world", size: 20),
100
- '<textarea id="body" name="body">\nhello world</textarea>'
101
- equal text_area_tag("body", "<b>hello world</b>", size: "20x40"),
102
- '<textarea cols="20" id="body" name="body" rows="40">\n&lt;b&gt;hello world&lt;/b&gt;</textarea>'
103
- equal text_area_tag("body", "<b>hello world</b>", size: "20x40", escape: false),
104
- '<textarea cols="20" id="body" name="body" rows="40">\n<b>hello world</b></textarea>'
105
- equal text_area_tag("body", null, escape: false),
106
- '<textarea id="body" name="body">\n</textarea>'
107
-
108
- test "check_box_tag", ->
109
- equal check_box_tag('admin'), '<input id="admin" name="admin" type="checkbox" value="1" />'
110
-
111
- test "radio_button_tag", ->
112
- equal radio_button_tag("people", "david"),
113
- '<input id="people_david" name="people" type="radio" value="david" />'
114
- equal radio_button_tag("num_people", 5),
115
- '<input id="num_people_5" name="num_people" type="radio" value="5" />'
116
- equal radio_button_tag("gender", "m") + radio_button_tag("gender", "f"),
117
- '<input id="gender_m" name="gender" type="radio" value="m" /><input id="gender_f" name="gender" type="radio" value="f" />'
118
- equal radio_button_tag("opinion", "-1") + radio_button_tag("opinion", "1"),
119
- '<input id="opinion_-1" name="opinion" type="radio" value="-1" /><input id="opinion_1" name="opinion" type="radio" value="1" />'
120
- equal radio_button_tag("person[gender]", "m"),
121
- '<input id="person_gender_m" name="person[gender]" type="radio" value="m" />'
122
- equal radio_button_tag('ctrlname', 'apache2.2'),
123
- '<input id="ctrlname_apache2.2" name="ctrlname" type="radio" value="apache2.2" />'
124
-
125
- test "submit_tag", ->
126
- equal submit_tag("Save", onclick: "alert('hello!')", data: { disable_with: "Saving..." }),
127
- '<input data-disable-with="Saving..." name="commit" onclick="alert(&apos;hello!&apos;)" type="submit" value="Save" />'
128
- equal submit_tag("Save", data: { disable_with: "Saving..." }),
129
- '<input data-disable-with="Saving..." name="commit" type="submit" value="Save" />'
130
- equal submit_tag("Save", data: { confirm: "Are you sure?" }),
131
- '<input data-confirm="Are you sure?" name="commit" type="submit" value="Save" />'
132
-
133
- test "button_tag", ->
134
- equal button_tag(),
135
- '<button name="button" type="submit">Button</button>'
136
- equal button_tag("Save", type: "submit"),
137
- '<button name="button" type="submit">Save</button>'
138
- equal button_tag("Button", type: "button"),
139
- '<button name="button" type="button">Button</button>'
140
- equal button_tag("Reset", type: "reset"),
141
- '<button name="button" type="reset">Reset</button>'
142
- equal button_tag("Reset", type: "reset", disabled: true),
143
- '<button disabled="disabled" name="button" type="reset">Reset</button>'
144
- equal button_tag("<b>Reset</b>", type: "reset", disabled: true),
145
- '<button disabled="disabled" name="button" type="reset">&lt;b&gt;Reset&lt;/b&gt;</button>'
146
- equal button_tag( -> 'Content' ),
147
- '<button name="button" type="submit">Content</button>'
148
- equal button_tag(name: 'temptation', type: 'button', -> content_tag('strong', 'Do not press me')),
149
- '<button name="temptation" type="button"><strong>Do not press me</strong></button>'
150
- equal button_tag("Save", type: "submit", data: { confirm: "Are you sure?" }),
151
- '<button data-confirm="Are you sure?" name="button" type="submit">Save</button>'
152
-
153
- test "image_submit_tag", ->
154
- equal image_submit_tag("save.gif", data: { confirm: "Are you sure?" }),
155
- '<input data-confirm="Are you sure?" src="/images/save.gif" type="image" />'
156
-
157
- test "field_set_tag", ->
158
- equal field_set_tag('Your details', -> 'Hello world!'),
159
- '<fieldset><legend>Your details</legend>Hello world!</fieldset>'
160
- equal field_set_tag(-> 'Hello world!'),
161
- '<fieldset>Hello world!</fieldset>'
162
- equal field_set_tag('', -> 'Hello world!'),
163
- '<fieldset>Hello world!</fieldset>'
164
- equal field_set_tag('', class: 'format', -> 'Hello world!'),
165
- '<fieldset class="format">Hello world!</fieldset>'
166
- equal field_set_tag(),
167
- '<fieldset></fieldset>'
168
- equal field_set_tag('You legend!'),
169
- '<fieldset><legend>You legend!</legend></fieldset>'
170
-
171
- test "different fields tags helpers", ->
172
- equal color_field_tag("car"),
173
- '<input id="car" name="car" type="color" />'
174
- equal search_field_tag("query"),
175
- '<input id="query" name="query" type="search" />'
176
- equal telephone_field_tag("cell"),
177
- '<input id="cell" name="cell" type="tel" />'
178
- equal telephone_field_tag("cell"),
179
- phone_field_tag("cell")
180
- equal date_field_tag("cell"),
181
- '<input id="cell" name="cell" type="date" />'
182
- equal time_field_tag("cell"),
183
- '<input id="cell" name="cell" type="time" />'
184
- equal datetime_field_tag("appointment"),
185
- '<input id="appointment" name="appointment" type="datetime" />'
186
- equal datetime_local_field_tag("appointment"),
187
- '<input id="appointment" name="appointment" type="datetime-local" />'
188
- equal month_field_tag("birthday"),
189
- '<input id="birthday" name="birthday" type="month" />'
190
- equal week_field_tag("birthday"),
191
- '<input id="birthday" name="birthday" type="week" />'
192
- equal url_field_tag("homepage"),
193
- '<input id="homepage" name="homepage" type="url" />'
194
- equal email_field_tag("address"),
195
- '<input id="address" name="address" type="email" />'
196
- equal number_field_tag("quantity", null, in: {min: "1", max: "9"}),
197
- '<input id="quantity" max="9" min="1" name="quantity" type="number" />'
198
- equal range_field_tag("volume", null, in: {min: 0, max: 11}, step: 0.1),
199
- '<input id="volume" max="11" min="0" name="volume" step="0.1" type="range" />'
200
-
201
- test "boolean options", ->
202
- equal check_box_tag("admin", 1, true, disabled: true, readonly: "yes"),
203
- '<input checked="checked" disabled="disabled" id="admin" name="admin" readonly="readonly" type="checkbox" value="1" />'
204
- equal check_box_tag("admin", 1, true, disabled: false, readonly: null),
205
- '<input checked="checked" id="admin" name="admin" type="checkbox" value="1" />'
206
- equal tag('input', type: "checkbox", checked: false),
207
- '<input type="checkbox" />'
208
- equal select_tag("people", "<option>david</option>", multiple: true),
209
- '<select id="people" multiple="multiple" name="people[]"><option>david</option></select>'
210
- equal select_tag("people[]", "<option>david</option>", multiple: true),
211
- '<select id="people_" multiple="multiple" name="people[]"><option>david</option></select>'
212
- equal select_tag("people", "<option>david</option>", multiple: null),
213
- '<select id="people" name="people"><option>david</option></select>'
214
-
215
- test "options side effects", ->
216
- options = { option: "random_option" }
217
- text_area_tag "body", "hello world", options
218
- deepEqual options, { option: "random_option" }
219
- submit_tag "submit value", options
220
- deepEqual options, { option: "random_option" }
221
- button_tag "button value", options
222
- deepEqual options, { option: "random_option" }
223
- image_submit_tag "submit source", options
224
- deepEqual options, { option: "random_option" }
225
- label_tag "submit source", "title", options
226
- deepEqual options, { option: "random_option" }
@@ -1,25 +0,0 @@
1
- #= require ultimate/underscore/underscore
2
- #= require ultimate/underscore/underscore.string
3
- #= require ultimate/underscore/underscore.outcasts
4
- #= require ultimate/helpers/javascript
5
-
6
- module "Ultimate.Helpers.Javascript"
7
-
8
- _.extend @, Ultimate.Helpers.Javascript
9
-
10
- test "escape_javascript", ->
11
- strictEqual escape_javascript(null), ''
12
- equal escape_javascript('This "thing" is really\n netos\''), 'This \\"thing\\" is really\\n netos\\\''
13
- equal escape_javascript('backslash\\test'), 'backslash\\\\test'
14
- equal escape_javascript('dont </close> tags'), 'dont <\\/close> tags'
15
- # equal escape_javascript('unicode \\342\\200\\250 newline'), 'unicode &#x2028; newline'
16
- # equal escape_javascript('unicode \\342\\200\\251 newline'), 'unicode &#x2029; newline'
17
- equal j('dont </close> tags'), 'dont <\\/close> tags'
18
- equal escape_javascript('\'quoted\' "double-quoted" new-line:\n </closed>'), '\\\'quoted\\\' \\"double-quoted\\" new-line:\\n <\\/closed>'
19
-
20
- test "javascript_tag", ->
21
- equal javascript_tag("alert('hello')"), "<script>\n//<![CDATA[\nalert('hello')\n//]]>\n</script>"
22
- equal javascript_tag("alert('hello')", id: "the_js_tag"), "<script id=\"the_js_tag\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>"
23
-
24
- test "javascript_cdata_section", ->
25
- equal javascript_cdata_section("alert('hello')"), "\n//<![CDATA[\nalert('hello')\n//]]>\n"
@@ -1,250 +0,0 @@
1
- #= require ultimate/underscore/underscore
2
- #= require ultimate/underscore/underscore.string
3
- #= require ultimate/improves/i18n-lite
4
- #= require ultimate/helpers/number
5
-
6
- module "Ultimate.Helpers.Number"
7
-
8
- _.extend @, Ultimate.Helpers.Number
9
-
10
- kilobytes = (number) -> number * 1024
11
- megabytes = (number) -> kilobytes(number) * 1024
12
- gigabytes = (number) -> megabytes(number) * 1024
13
- terabytes = (number) -> gigabytes(number) * 1024
14
-
15
- test "number_to_phone", ->
16
- equal number_to_phone(5551234), "555-1234"
17
- equal number_to_phone(8005551212), "800-555-1212"
18
- equal number_to_phone(8005551212, area_code: true), "(800) 555-1212"
19
- strictEqual number_to_phone("", area_code: true), ""
20
- equal number_to_phone(8005551212, delimiter: " "), "800 555 1212"
21
- equal number_to_phone(8005551212, area_code: true, extension: 123), "(800) 555-1212 x 123"
22
- equal number_to_phone(8005551212, extension: " "), "800-555-1212"
23
- strictEqual number_to_phone(5551212, delimiter: '.'), "555.1212"
24
- equal number_to_phone("8005551212"), "800-555-1212"
25
- equal number_to_phone(8005551212, country_code: 1), "+1-800-555-1212"
26
- strictEqual number_to_phone(8005551212, country_code: 1, delimiter: ''), "+18005551212"
27
- equal number_to_phone(225551212), "22-555-1212"
28
- equal number_to_phone(225551212, country_code: 45), "+45-22-555-1212"
29
- equal number_to_phone(911), "911"
30
- equal number_to_phone(0), "0"
31
- equal number_to_phone(-1), "1"
32
-
33
- test "number_to_currency", ->
34
- equal number_to_currency(1234567890.50), "$1,234,567,890.50"
35
- equal number_to_currency(1234567890.506), "$1,234,567,890.51"
36
- equal number_to_currency(-1234567890.50), "-$1,234,567,890.50"
37
- equal number_to_currency(-1234567890.50, format: "%u %n"), "-$ 1,234,567,890.50"
38
- equal number_to_currency(-1234567890.50, negative_format: "(%u%n)"), "($1,234,567,890.50)"
39
- equal number_to_currency(1234567891.50, precision: 0), "$1,234,567,892"
40
- equal number_to_currency(1234567890.50, precision: 1), "$1,234,567,890.5"
41
- equal number_to_currency(1234567890.50, unit: "&pound;", separator: ",", delimiter: ""), "&pound;1234567890,50"
42
- equal number_to_currency("1234567890.50"), "$1,234,567,890.50"
43
- equal number_to_currency("1234567890.50", unit: "K&#269;", format: "%n %u"), "1,234,567,890.50 K&#269;"
44
- equal number_to_currency("-1234567890.50", unit: "K&#269;", format: "%n %u", negative_format: "%n - %u"), "1,234,567,890.50 - K&#269;"
45
- strictEqual number_to_currency(0.0, unit: "", negative_format: "(%n)"), "0.00"
46
-
47
- test "number_to_percentage", ->
48
- equal number_to_percentage(100), "100.000%"
49
- equal number_to_percentage(100, precision: 0), "100%"
50
- equal number_to_percentage(302.0574, precision: 2), "302.06%"
51
- equal number_to_percentage("100"), "100.000%"
52
- equal number_to_percentage("1000"), "1000.000%"
53
- equal number_to_percentage(123.400, precision: 3, strip_insignificant_zeros: true), "123.4%"
54
- equal number_to_percentage(1000, delimiter: '.', separator: ','), "1.000,000%"
55
- equal number_to_percentage(1000, format: "%n %"), "1000.000 %"
56
- equal number_to_percentage(0), "0.000%"
57
- equal number_to_percentage(-1), "-1.000%"
58
-
59
- test "number_to_delimited", ->
60
- strictEqual number_to_delimited(12345678), "12,345,678"
61
- strictEqual number_to_delimited(0), "0"
62
- strictEqual number_to_delimited(123), "123"
63
- strictEqual number_to_delimited(123456), "123,456"
64
- strictEqual number_to_delimited(123456.78), "123,456.78"
65
- strictEqual number_to_delimited(123456.789), "123,456.789"
66
- strictEqual number_to_delimited(123456.78901), "123,456.78901"
67
- strictEqual number_to_delimited(123456789.78901), "123,456,789.78901"
68
- strictEqual number_to_delimited(0.78901), "0.78901"
69
- strictEqual number_to_delimited("123456.78"), "123,456.78"
70
- strictEqual number_to_delimited(12345678, delimiter: ' '), "12 345 678"
71
- strictEqual number_to_delimited(12345678.05, separator: '-'), "12,345,678-05"
72
- strictEqual number_to_delimited(12345678.05, separator: ',', delimiter: '.'), "12.345.678,05"
73
- strictEqual number_to_delimited(12345678.05, delimiter: '.', separator: ','), "12.345.678,05"
74
-
75
- test "round_with_precision", ->
76
- strictEqual round_with_precision(10/3), 3.33
77
- strictEqual round_with_precision(10/3, 0), 3
78
- strictEqual round_with_precision(100/3, 5), 33.33333
79
- strictEqual round_with_precision(100/3, -1), 30
80
- strictEqual round_with_precision("19.3", -1), 20
81
- # strictEqual round_with_precision(9.995, 2), 10
82
- strictEqual round_with_precision(null), 0
83
-
84
- test "number_to_rounded", ->
85
- strictEqual number_to_rounded(-111.2346), "-111.235"
86
- strictEqual number_to_rounded(111.2346), "111.235"
87
- strictEqual number_to_rounded(31.825, precision: 2), "31.83"
88
- strictEqual number_to_rounded(111.2346, precision: 2), "111.23"
89
- strictEqual number_to_rounded(111, precision: 2), "111.00"
90
- strictEqual number_to_rounded("111.2346"), "111.235"
91
- strictEqual number_to_rounded("31.825", precision: 2), "31.83"
92
- strictEqual number_to_rounded((32.6751 * 100.00), precision: 0), "3268"
93
- strictEqual number_to_rounded(111.50, precision: 0), "112"
94
- strictEqual number_to_rounded(1234567891.50, precision: 0), "1234567892"
95
- strictEqual number_to_rounded(0, precision: 0), "0"
96
- strictEqual number_to_rounded(0.001, precision: 5), "0.00100"
97
- strictEqual number_to_rounded(0.00111, precision: 3), "0.001"
98
- # strictEqual number_to_rounded(9.995, precision: 2), "10.00"
99
- strictEqual number_to_rounded(10.995, precision: 2), "11.00"
100
- strictEqual number_to_rounded(-0.001, precision: 2), "0.00"
101
- strictEqual number_to_rounded(31.825, precision: 2, separator: ','), "31,83"
102
- strictEqual number_to_rounded(1231.825, precision: 2, separator: ',', delimiter: '.'), "1.231,83"
103
- strictEqual number_to_rounded(123987, precision: 3, significant: true), "124000"
104
- strictEqual number_to_rounded(123987876, precision: 2, significant: true ), "120000000"
105
- strictEqual number_to_rounded("43523", precision: 1, significant: true ), "40000"
106
- strictEqual number_to_rounded(9775, precision: 4, significant: true ), "9775"
107
- strictEqual number_to_rounded(5.3923, precision: 2, significant: true ), "5.4"
108
- strictEqual number_to_rounded(5.3923, precision: 1, significant: true ), "5"
109
- strictEqual number_to_rounded(1.232, precision: 1, significant: true ), "1"
110
- strictEqual number_to_rounded(7, precision: 1, significant: true ), "7"
111
- strictEqual number_to_rounded(1, precision: 1, significant: true ), "1"
112
- strictEqual number_to_rounded(52.7923, precision: 2, significant: true ), "53"
113
- strictEqual number_to_rounded(9775, precision: 6, significant: true ), "9775.00"
114
- strictEqual number_to_rounded(5.3929, precision: 7, significant: true ), "5.392900"
115
- strictEqual number_to_rounded(0, precision: 2, significant: true ), "0.0"
116
- strictEqual number_to_rounded(0, precision: 1, significant: true ), "0"
117
- strictEqual number_to_rounded(0.0001, precision: 1, significant: true ), "0.0001"
118
- strictEqual number_to_rounded(0.0001, precision: 3, significant: true ), "0.000100"
119
- strictEqual number_to_rounded(0.0001111, precision: 1, significant: true ), "0.0001"
120
- # strictEqual number_to_rounded(9.995, precision: 3, significant: true), "10.0"
121
- strictEqual number_to_rounded(9.994, precision: 3, significant: true), "9.99"
122
- strictEqual number_to_rounded(10.995, precision: 3, significant: true), "11.0"
123
- strictEqual number_to_rounded(9775.43, precision: 4, strip_insignificant_zeros: true ), "9775.43"
124
- strictEqual number_to_rounded(9775.2, precision: 6, significant: true, strip_insignificant_zeros: true ), "9775.2"
125
- strictEqual number_to_rounded(0, precision: 6, significant: true, strip_insignificant_zeros: true ), "0"
126
- strictEqual number_to_rounded(123.987, precision: 0, significant: true), "124"
127
- strictEqual number_to_rounded(12, precision: 0, significant: true ), "12"
128
- strictEqual number_to_rounded("12.3", precision: 0, significant: true ), "12"
129
-
130
- test "number_to_human_size", ->
131
- equal number_to_human_size(0), "0 Bytes"
132
- # equal number_to_human_size(1), "1 Byte"
133
- equal number_to_human_size(3.14159265), "3 Bytes"
134
- equal number_to_human_size(123.0), "123 Bytes"
135
- equal number_to_human_size(123), "123 Bytes"
136
- equal number_to_human_size(1234), "1.21 KB"
137
- equal number_to_human_size(12345), "12.1 KB"
138
- equal number_to_human_size(1234567), "1.18 MB"
139
- equal number_to_human_size(1234567890), "1.15 GB"
140
- equal number_to_human_size(1234567890123), "1.12 TB"
141
- equal number_to_human_size(terabytes(1026)), "1030 TB"
142
- equal number_to_human_size(kilobytes(444)), "444 KB"
143
- equal number_to_human_size(megabytes(1023)), "1020 MB"
144
- equal number_to_human_size(terabytes(3)), "3 TB"
145
- equal number_to_human_size(1234567, precision: 2), "1.2 MB"
146
- equal number_to_human_size(3.14159265, precision: 4), "3 Bytes"
147
- equal number_to_human_size('123'), "123 Bytes"
148
- equal number_to_human_size(kilobytes(1.0123), precision: 2), "1 KB"
149
- equal number_to_human_size(kilobytes(1.0100), precision: 4), "1.01 KB"
150
- equal number_to_human_size(kilobytes(10.000), precision: 4), "10 KB"
151
- # equal number_to_human_size(1.1), "1 Byte"
152
- equal number_to_human_size(10), "10 Bytes"
153
- equal number_to_human_size(3.14159265, prefix: 'si'), "3 Bytes"
154
- equal number_to_human_size(123.0, prefix: 'si'), "123 Bytes"
155
- equal number_to_human_size(123, prefix: 'si'), "123 Bytes"
156
- equal number_to_human_size(1234, prefix: 'si'), "1.23 KB"
157
- equal number_to_human_size(12345, prefix: 'si'), "12.3 KB"
158
- equal number_to_human_size(1234567, prefix: 'si'), "1.23 MB"
159
- equal number_to_human_size(1234567890, prefix: 'si'), "1.23 GB"
160
- equal number_to_human_size(1234567890123, prefix: 'si'), "1.23 TB"
161
- equal number_to_human_size(1234567, precision: 2), "1.2 MB"
162
- equal number_to_human_size(3.14159265, precision: 4), "3 Bytes"
163
- equal number_to_human_size(kilobytes(1.0123), precision: 2), "1 KB"
164
- equal number_to_human_size(kilobytes(1.0100), precision: 4), "1.01 KB"
165
- equal number_to_human_size(kilobytes(10.000), precision: 4), "10 KB"
166
- equal number_to_human_size(1234567890123, precision: 1), "1 TB"
167
- equal number_to_human_size(524288000, precision: 3), "500 MB"
168
- equal number_to_human_size(9961472, precision: 0), "10 MB"
169
- equal number_to_human_size(41010, precision: 1), "40 KB"
170
- equal number_to_human_size(41100, precision: 2), "40 KB"
171
- equal number_to_human_size(kilobytes(1.0123), precision: 2, strip_insignificant_zeros: false), "1.0 KB"
172
- equal number_to_human_size(kilobytes(1.0123), precision: 3, significant: false), "1.012 KB"
173
- equal number_to_human_size(kilobytes(1.0123), precision: 0, significant: true), "1 KB" #ignores significant it precision is 0
174
- equal number_to_human_size(kilobytes(1.0123), precision: 3, separator: ','), "1,01 KB"
175
- equal number_to_human_size(kilobytes(1.0100), precision: 4, separator: ','), "1,01 KB"
176
- equal number_to_human_size(terabytes(1000.1), precision: 5, delimiter: '.', separator: ','), "1.000,1 TB"
177
-
178
- test "number_to_human", ->
179
- strictEqual number_to_human(-123), "-123"
180
- strictEqual number_to_human(-0.5), "-0.5"
181
- strictEqual number_to_human(0), "0"
182
- strictEqual number_to_human(0.5), "0.5"
183
- strictEqual number_to_human(123), "123"
184
- equal number_to_human(1234), "1.23 Thousand"
185
- equal number_to_human(12345), "12.3 Thousand"
186
- equal number_to_human(1234567), "1.23 Million"
187
- equal number_to_human(1234567890), "1.23 Billion"
188
- equal number_to_human(1234567890123), "1.23 Trillion"
189
- equal number_to_human(1234567890123456), "1.23 Quadrillion"
190
- equal number_to_human(1234567890123456789), "1230 Quadrillion"
191
- equal number_to_human(489939, precision: 2), "490 Thousand"
192
- equal number_to_human(489939, precision: 4), "489.9 Thousand"
193
- equal number_to_human(489000, precision: 4), "489 Thousand"
194
- equal number_to_human(489000, precision: 4, strip_insignificant_zeros: false), "489.0 Thousand"
195
- equal number_to_human(1234567, precision: 4, significant: false), "1.2346 Million"
196
- equal number_to_human(1234567, precision: 1, significant: false, separator: ','), "1,2 Million"
197
- equal number_to_human(1234567, precision: 0, significant: true, separator: ','), "1 Million" #significant forced to false
198
- #Only integers
199
- volume = unit: "ml", thousand: "lt", million: "m3"
200
- equal number_to_human(123456, units: volume), "123 lt"
201
- equal number_to_human(12, units: volume), "12 ml"
202
- equal number_to_human(1234567, units: volume), "1.23 m3"
203
- #Including fractionals
204
- distance = mili: "mm", centi: "cm", deci: "dm", unit: "m", ten: "dam", hundred: "hm", thousand: "km"
205
- equal number_to_human(0.00123, units: distance), "1.23 mm"
206
- equal number_to_human(0.0123, units: distance), "1.23 cm"
207
- equal number_to_human(0.123, units: distance), "1.23 dm"
208
- equal number_to_human(1.23, units: distance), "1.23 m"
209
- equal number_to_human(12.3, units: distance), "1.23 dam"
210
- equal number_to_human(123, units: distance), "1.23 hm"
211
- equal number_to_human(1230, units: distance), "1.23 km"
212
- equal number_to_human(1230, units: distance), "1.23 km"
213
- equal number_to_human(1230, units: distance), "1.23 km"
214
- equal number_to_human(12300, units: distance), "12.3 km"
215
- #The quantifiers don't need to be a continuous sequence
216
- gangster = hundred: "hundred bucks", million: "thousand quids"
217
- equal number_to_human(100, units: gangster), "1 hundred bucks"
218
- equal number_to_human(2500, units: gangster), "25 hundred bucks"
219
- equal number_to_human(25000000, units: gangster), "25 thousand quids"
220
- equal number_to_human(12345000000, units: gangster), "12300 thousand quids"
221
- #Spaces are stripped from the resulting string
222
- strictEqual number_to_human(4, units: unit: "", ten: 'tens '), "4"
223
- equal number_to_human(45, units: unit: "", ten: ' tens '), "4.5 tens"
224
- equal number_to_human(123456, format: "%n times %u"), "123 times Thousand"
225
- volume = unit: "ml", thousand: "lt", million: "m3"
226
- equal number_to_human(123456, units: volume, format: "%n.%u"), "123.lt"
227
-
228
- number_to_helpers = _.string.words("phone currency percentage delimited rounded human_size human")
229
-
230
- test "number helpers should return empty string when given null", number_to_helpers.length, =>
231
- for h in number_to_helpers
232
- strictEqual @["number_to_#{h}"](null), "", "number_to_#{h}"
233
-
234
- test "number helpers don't mutate options hash", number_to_helpers.length, =>
235
- options = raise: true
236
- for h in number_to_helpers
237
- @["number_to_#{h}"](1, options)
238
- deepEqual options, {raise: true}, "number_to_#{h}"
239
-
240
- test "number helpers should return non numeric param unchanged", ->
241
- equal number_to_phone("x", country_code: 1, extension: 123), "+1-x x 123"
242
- equal number_to_phone("x"), "x"
243
- equal number_to_currency("x."), "$x."
244
- equal number_to_currency("x"), "$x"
245
- equal number_to_percentage("x"), "x%"
246
- equal number_to_delimited("x"), "x"
247
- equal number_to_rounded("x."), "x."
248
- equal number_to_rounded("x"), "x"
249
- equal number_to_human_size('x'), "x"
250
- equal number_to_human('x'), "x"