forked from swannodette/mori
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain-spec.js
152 lines (90 loc) · 3.61 KB
/
chain-spec.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
/*
var mori = require("../mori")
, c = mori.chain
, _ = c._;
describe("ChainedMap", function () {
it("demonstrates mapping a function over a vector", function () {
var inc = function (n) { return n + 1; },
outArr = _([1,2,3,4,5]).map(inc).out();
expect(outArr).toEqual([2,3,4,5,6]);
});
});
describe("Non-destructive update", function () {
it("demonstrates a non-destructive collection update", function () {
var v1 = _([1,2,3]),
v2 = v1.conj(4);
expect(JSON.stringify(v1.out())).toEqual("[1,2,3]");
expect(JSON.stringify(v2.out())).toEqual("[1,2,3,4]");
});
});
describe("Reduce", function () {
it("demonstrates functional reduction on a vector", function () {
var sum = function(a, b) {
return a + b;
};
var reduced = _([1, 2, 3, 4]).reduce(sum, 0).out();
expect(reduced).toEqual(10);
});
});
describe("Lazy sequences", function () {
it("demonstrates interposition on a lazy sequence", function () {
var interposed = _([1, 2, 3, 4]).interpose("foo").out();
expect(interposed).toEqual([1, "foo", 2, "foo", 3, "foo", 4]);
});
});
describe("Conversion utilities", function () {
it("demonstrates conversion from clojurescript values to javascript objects, and vice versa", function () {
var jsObj = { a: 1, b: "two" },
jsArr = [1, 2, 3],
cljMap = _({a: 1, b: "two"}),
cljMapKeywordized = mori.hashMap(mori.keyword("a"), 1, mori.keyword("b"), "two"),
cljVec = _([1, 2, 3]);
expect(_(jsObj).is(cljMap)).toBe(true);
expect(_(jsObj,true).is(cljMapKeywordized)).toBe(true);
expect(_(jsObj).is(jsObj)).toBe(false);
expect(_(jsArr).is(cljVec)).toBe(true);
expect(_(cljMap).out()).toEqual(jsObj);
expect(_(cljVec).out()).toEqual(jsArr);
expect(_(jsArr).isVector()).toBe(true);
});
});
describe("Distinct", function() {
it("demonstrates function `distinct`", function() {
var vec = _([1,1,1,1,2,2,3,4,5,6,6]);
var distinctVector = vec.distinct();
expect(distinctVector.out()).toEqual([1,2,3,4,5,6]);
});
});
describe("Predicates", function() {
it("demonstrates predicate usage", function() {
expect(_([]).isEmpty()).toBe(true);
expect(_([1]).isEmpty()).toBe(false);
expect(_([]).isList()).toBe(false);
expect(_([1,2]).toSeq().isSeq()).toBe(true);
expect(_([]).toSeq().isEmpty()).toBe(true);
expect(_([1,2]).isVector()).toBe(true);
expect(_([1,2]).toSet().isVector()).toBe(false);
expect(_({a: 1, b: "two"}).isMap()).toBe(true);
expect(_([1,2]).isMap()).toBe(false);
expect(_([1,2,2]).toSet().isSet()).toBe(true);
expect(_([]).isSet()).toBe(false);
expect(_([1,2]).isCollection()).toBe(true);
expect(_(1).isCollection()).toBe(false);
expect(_([1,2]).isSequential()).toBe(true);
expect(_(1).isSequential()).toBe(false);
expect(_([1,2]).isAssociative()).toBe(true);
expect(_([]).map(mori.identity).isAssociative()).toBe(false);
expect(_([]).isCounted()).toBe(true);
expect(_(1).isCounted()).toBe(false);
expect(_([]).isIndexed()).toBe(true);
expect(_([]).map(mori.identity).isIndexed()).toBe(false);
expect(_([]).isReduceable()).toBe(true);
expect(_(1).isReduceable()).toBe(false);
expect(_([]).isSeqable()).toBe(true);
expect(_([1]).isSeqable()).toBe(true);
expect(_([]).isReversible()).toBe(true);
expect(_(1).isReversible()).toBe(false);
expect(_([]).isChainable()).toBe(true);
});
});
*/