-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathutil.strings.js
More file actions
70 lines (59 loc) · 3.14 KB
/
util.strings.js
File metadata and controls
70 lines (59 loc) · 3.14 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
$(document).ready(function() {
QUnit.module('underscore.util.strings');
QUnit.test('explode', function(assert) {
assert.deepEqual(_.explode('Virgil'), ['V','i','r','g','i','l'], 'Should explode a string into an array of characters.');
});
QUnit.test('fromQuery', function(assert) {
var query = 'foo%5Bbar%5D%5Bbaz%5D%5Bblargl%5D=blah&foo%5Bbar%5D%5Bbaz%5D%5Bblargr%5D=woop&blar=bluh&abc[]=123&abc[]=234';
assert.ok(_.isEqual(_.fromQuery(query), {
'foo': {
'bar': {
'baz': {
'blargl': 'blah',
'blargr': 'woop'
}
}
},
'blar': 'bluh',
'abc': [
'123',
'234'
]
}), 'can convert a query string to a hash');
});
QUnit.test('implode', function(assert) {
assert.equal(_.implode(['H','o','m','e','r']), 'Homer', 'Should implode an array of characters into a single string.');
});
QUnit.test('isTrueish', function(assert) {
assert.equal(_.isTrueish(), false, 'should return false');
assert.equal(_.isTrueish(undefined), false, 'should return false');
assert.equal(_.isTrueish(null), false, 'should return false');
assert.equal(_.isTrueish(true), true, 'should return true');
assert.equal(_.isTrueish(false), false, 'should return false');
assert.equal(_.isTrueish('true'), true, 'should return true');
assert.equal(_.isTrueish('false'), false, 'should return false');
assert.equal(_.isTrueish('True'), true, 'should return true');
assert.equal(_.isTrueish('FaLsE'), false, 'should return false');
});
QUnit.test('camelCase', function(assert) {
assert.equal(_.camelCase('punic-wars'), 'punicWars', 'Should convert a dashed-format string to camelCase.');
});
QUnit.test('toDash', function(assert) {
assert.equal(_.toDash('trojanWar'), 'trojan-war', 'Should convert a camelCase string to dashed-format.');
assert.equal(_.toDash('PersianWar'), 'persian-war', 'Should convert a PascalCase string to dashed-format.');
});
QUnit.test('toQuery', function(assert) {
var obj = {'foo&bar': 'baz', 'test': 'total success', 'nested': {'works': 'too'}, 'isn\'t': ['that', 'cool?']};
assert.equal(_.toQuery(obj), 'foo%26bar=baz&test=total%20success&nested%5Bworks%5D=too&isn\'t%5B%5D=that&isn\'t%5B%5D=cool%3F', 'can convert a hash to a query string');
assert.equal(_.toQuery(obj), jQuery.param(obj), 'query serialization matchs jQuery.param()');
assert.equal(_.toQuery({a: []}), '', 'empty array params produce the empty string');
assert.equal(_.toQuery({a: [], b: []}), '', 'multiple empty array params do not lead to spurious ampersands');
assert.equal(_.toQuery({a: null, b: undefined}), 'a=null&b=undefined', 'respects null and undefined');
});
QUnit.test('strContains', function(assert) {
assert.equal(_.strContains('Metaphysics', 'physics'), true, 'Should return true if string contains search string.');
assert.equal(_.strContains('Poetics', 'prose'), false, 'Should return false if string does not contain search string.');
var thrower = function() { _.strContains([], ''); };
assert.throws(thrower, TypeError, 'Throws TypeError if first argument is not a string.');
});
});