Skip to content

Commit ca15fe4

Browse files
author
Chris Myers
committed
Merge branch 'release/0.8.3'
2 parents 3641b13 + ec7826a commit ca15fe4

File tree

8 files changed

+74
-43
lines changed

8 files changed

+74
-43
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "monet",
3-
"version": "0.8.2",
3+
"version": "0.8.3",
44
"main": ["src/main/javascript/monet.js","src/main/javascript/monet-pimp.js"],
55
"ignore": [
66
"**/.*",

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "monet",
3-
"version": "0.8.2",
3+
"version": "0.8.3",
44
"scripts": [
55
"src/main/javascript/monet.js",
66
"src/main/javascript/monet-pimp.js"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Chris Myers",
33
"name": "monet",
44
"description": "Monadic types library for JavaScript",
5-
"version": "0.8.2",
5+
"version": "0.8.3",
66
"homepage": "https://github.com/cwmyers/monet.js",
77
"repository": {
88
"type": "git",

src/main/javascript/monet-pimp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// monet-pimp.js 0.8.2
1+
// monet-pimp.js 0.8.3
22

33
// This file needs to be included after monet.js
44

src/main/javascript/monet.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Monet.js 0.8.2
1+
// Monet.js 0.8.3
22

33
// (c) 2012-2014 Chris Myers
44
// Monet.js may be freely distributed under the MIT license.
@@ -25,7 +25,7 @@
2525
};
2626

2727

28-
var isFunction = function (f) {
28+
var isFunction = function (f) {
2929
return !!(f && f.constructor && f.call && f.apply)
3030
};
3131

@@ -147,6 +147,12 @@
147147
return list.foldLeft(Nil)(swap(cons))
148148
}
149149

150+
var listFilter = function(list, fn) {
151+
return list.foldRight(Nil)(function(a, acc) {
152+
return fn(a) ? cons(a,acc): acc
153+
})
154+
}
155+
150156
var cons = function (head, tail) {
151157
return tail.cons(head)
152158
}
@@ -200,6 +206,9 @@
200206
append: function (list2) {
201207
return append(this, list2)
202208
},
209+
filter: function(fn) {
210+
return listFilter(this, fn)
211+
},
203212
flatten: function () {
204213
return foldRight(append, this, Nil)
205214
},
@@ -345,6 +354,9 @@
345354
foldLeft: function (initialValue) {
346355
return this.toList().foldLeft(initialValue)
347356
},
357+
filter: function (fn) {
358+
return listFilter(this.toList(), fn)
359+
},
348360
append: function (list2) {
349361
return NEL.fromList(this.toList().append(list2.toList())).some()
350362
},

src/test/javascript/list_spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,22 @@ describe("An immutable list", function () {
196196
it("will return None for headMaybe", function () {
197197
expect(Nil.headMaybe()).toBeNoneMaybe()
198198
})
199+
it("will return an empty list on filter", function() {
200+
expect(Nil.filter(function() {return true})).toEqual(Nil)
201+
})
199202
})
200203

201204
describe("that has multiple elements", function () {
202205
it("will return the first element in a Some for headMaybe", function () {
203206
expect(list.headMaybe()).toBeSomeMaybeWith(1)
204207

205208
})
209+
it("will return the elements that pass the filter", function () {
210+
expect(list.filter(function (a) {return a%2==0}).toArray()).toEqual([2,4])
211+
212+
})
206213
})
214+
207215
describe("that is huge, must not blow the stack", function () {
208216
var list1 = [], list2 = []
209217
var size = 1000;

src/test/javascript/nel_spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,11 @@ describe("A Non-Empty immutable list", function () {
7676
expect(nAppend.toArray()).toEqual([1,2,3,4,5,6,7,8])
7777
})
7878

79+
it("will be filtered", function() {
80+
var n1 = NEL.fromList([1,2,3,4,5,6,7,8].list()).some()
81+
expect(n1.filter(function(a){return a%2==0}).toArray()).toEqual([2,4,6,8])
82+
83+
})
84+
7985
})
8086

src/test/javascript/reader_spec.js

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,52 @@
11
describe("A Reader Monad", function () {
22

3-
var config1 = {
4-
url: "http://test1.com",
5-
port: 8080,
6-
key: "s3cr3t"
7-
}
8-
var config2 = {
9-
url: "http://test2.com",
10-
port: 8081
11-
}
12-
var connect = function (endpoint, config) {
13-
return "POST " + config.url + ":" + config.port + "/" + endpoint
14-
}.reader()
15-
16-
17-
it("must inject config with run()", function () {
18-
expect(connect("user").run(config1)).toBe("POST http://test1.com:8080/user")
19-
expect(connect("session").run(config2)).toBe("POST http://test2.com:8081/session")
3+
var config1 = {
4+
url: "http://test1.com",
5+
port: 8080,
6+
key: "s3cr3t"
7+
}
8+
var config2 = {
9+
url: "http://test2.com",
10+
port: 8081
11+
}
12+
var connect = function (endpoint, config) {
13+
return "POST " + config.url + ":" + config.port + "/" + endpoint
14+
}.reader()
15+
16+
var format = function (url) {
17+
return "!" + url + "!"
18+
}.reader()
19+
20+
it("must inject config with run()", function () {
21+
expect(connect("user").run(config1)).toBe("POST http://test1.com:8080/user")
22+
expect(connect("session").run(config2)).toBe("POST http://test2.com:8081/session")
23+
})
24+
25+
it("must be mappable", function () {
26+
var reader = connect("session").map(function (s) {
27+
return s + "?secure=true"
2028
})
29+
expect(reader.run(config1)).toBe("POST http://test1.com:8080/session?secure=true")
2130

22-
it("must be mappable", function() {
23-
var reader = connect("session").map(function (s) {return s + "?secure=true"})
24-
expect(reader.run(config1)).toBe("POST http://test1.com:8080/session?secure=true")
31+
})
2532

26-
})
27-
28-
it("must be flatMappable", function() {
29-
var reader = connect("something").flatMap(
30-
function(connectionString, config){
31-
return connectionString+"?secretKey="+config.key
32-
}.reader())
33-
expect(reader.run(config1)).toBe("POST http://test1.com:8080/something?secretKey=s3cr3t")
34-
})
33+
it("must be flatMappable", function () {
34+
var reader = connect("something").flatMap(
35+
function (connectionString, config) {
36+
return connectionString + "?secretKey=" + config.key
37+
}.reader())
38+
expect(reader.run(config1)).toBe("POST http://test1.com:8080/something?secretKey=s3cr3t")
39+
})
3540

36-
it("must be applicable", function() {
37-
var reader = connect("something").ap(Reader(function(config) {
38-
return function(c) {
39-
return "**"+c+"**"
40-
}
41+
it("must be applicable", function () {
42+
var reader = connect("something").ap(Reader(function (config) {
43+
return function (c) {
44+
return "**" + c + "**"
45+
}
4146

42-
}))
43-
expect(reader.run(config1)).toBe("**POST http://test1.com:8080/something**")
47+
}))
48+
expect(reader.run(config1)).toBe("**POST http://test1.com:8080/something**")
4449

45-
})
50+
})
4651

4752
})

0 commit comments

Comments
 (0)