Skip to content

Commit 231e3dd

Browse files
committed
implement mutable ITopic protocol
1 parent 0d40d8d commit 231e3dd

File tree

15 files changed

+60
-32
lines changed

15 files changed

+60
-32
lines changed

src/core/core.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ export function either(memo, value){
455455

456456
export const isIdentical = Object.is;
457457

458+
export const looseEq = (a, b) => a == b;
459+
458460
export function everyPred(...preds){
459461
return function(){
460462
return fold(function(memo, arg){

src/core/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export * from "./core.js";
1515
export * from "./types.js";
1616
export * from "./protocols.js";
1717
export * from "./protocols/concrete.js";
18-
export {sequence, blot, blottable, reduceWith, reducekvWith} from "./shared.js";
18+
export {sequence, blot, blottable, itopic, reduceWith, reducekvWith} from "./shared.js";
1919
import * as p from "./protocols/concrete.js";
2020
import * as T from "./types.js";
2121
import {extend, forward} from "./types/protocol/concrete.js";

src/core/protocols/iassociative/concrete.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ function updateInN(self, keys, f) {
9393
export const updateIn = overload(null, null, null, updateIn3, updateIn4, updateIn5, updateIn6, updateInN);
9494

9595
function contains3(self, key, value){
96-
return IAssociative.contains(self, key) && equiv(get(self, key), value);
96+
const equals = IAssociative.contains(self) || equiv;
97+
return IAssociative.contains(self, key) && equals(get(self, key), value);
9798
}
9899

99-
export const contains = overload(null, null, IAssociative.contains, contains3);
100+
export const contains = overload(null, IAssociative.contains, IAssociative.contains, contains3);
100101
export const rewrite = branch(IAssociative.contains, update, identity);
101102
export const prop = overload(null, function(key){
102103
return overload(null, v => get(v, key), v => assoc(v, key, v));

src/core/shared.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,35 @@ import * as s from "./protocols/iseq/concrete.js"
44
import {seq} from "./protocols/iseqable/concrete.js";
55
import {compact} from "./protocols/icompactible/concrete.js";
66
import {get} from "./protocols/ilookup/concrete.js";
7+
import {contains} from "./protocols/iassociative/concrete.js";
78
import {equiv} from "./protocols/iequiv/concrete.js";
89
import {keys, dissoc} from "./protocols/imap/concrete.js";
910
import {lazySeq} from "./types/lazy-seq/construct.js";
10-
import {map, mapIndexed} from "./types/lazy-seq/concrete.js";
11+
import {map, mapcat, mapIndexed} from "./types/lazy-seq/concrete.js";
1112
import {cons} from "./types/list/construct.js";
1213
import {emptyList} from "./types/empty-list/construct.js";
1314
import {array} from "./types/array/construct.js";
1415

16+
export function itopic(assoc, dissoc, {equals = equiv, assertArity1 = identity, assertArity2 = identity} = {}){
17+
function assert2(self, key){
18+
return contains(self, key) ? [[key, get(self, key)]] : null;
19+
}
20+
21+
function assert1(self){
22+
return seq(mapcat(assert2(self, ?), keys(self)));
23+
}
24+
25+
const assert = overload(null, assertArity1(assert1), assertArity2(assert2), assoc);
26+
27+
function retract3(self, key, value){
28+
return equals(get(self, key), value) ? dissoc(self, key) : self;
29+
}
30+
31+
const retract = overload(null, null, dissoc, retract3);
32+
33+
return {assert, retract}
34+
}
35+
1536
export function blot(self){
1637
return seq(compact(self)) ? self : null;
1738
}

src/core/types/object/behave.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {emptyObject} from "../object/construct.js";
1212
import {descriptive} from "../object/concrete.js";
1313
import {keying} from "../../protocols/imapentry/concrete.js";
1414
import {hashKeyed as hash} from "../../protocols/ihashable/hashers.js";
15-
import {reduceWith, reducekvWith} from "../../shared.js";
15+
import {reduceWith, reducekvWith, itopic} from "../../shared.js";
1616
import * as p from "./protocols.js";
1717

1818
const keys = Object.keys;
@@ -101,28 +101,13 @@ function clone(self){
101101
return Object.assign({}, self);
102102
}
103103

104-
function assert2(self, key){
105-
return p.contains(self, key) ? [[key, p.get(self, key)]] : null;
106-
}
107-
108-
function assert1(self){
109-
return p.seq(mapcat(assert2(self, ?), p.keys(self)));
110-
}
111-
112-
export const assert = overload(null, assert1, assert2, p.assoc);
113-
114-
function retract3(self, key, value){
115-
return p.equiv(p.get(self, key), value) ? p.dissoc(self, key) : self;
116-
}
117-
118-
export const retract = overload(null, null, p.dissoc, retract3);
119-
120104
const reduce = reduceWith(p.seq);
121105
const reducekv = reducekvWith(p.seq);
122106
const count = comp(p.count, p.keys);
123107

124108
export default does(
125109
keying("Object"),
110+
implement(ITopic, itopic(p.assoc, p.dissoc)),
126111
implement(IHashable, {hash}),
127112
implement(IMergable, {merge}),
128113
implement(ICompactible, {compact}),
@@ -138,7 +123,6 @@ export default does(
138123
implement(IFn, {invoke: lookup}),
139124
implement(ISeq, {first, rest}),
140125
implement(ILookup, {lookup}),
141-
implement(ITopic, {assert, retract}),
142126
implement(IEmptyableCollection, {empty: emptyObject}),
143127
implement(IAssociative, {assoc, contains}),
144128
implement(ISeqable, {seq}),

src/dom/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ function attr2(self, key){
7575
if (_.isString(key)) {
7676
return self.getAttribute(key);
7777
} else {
78-
const pairs = key;
79-
$.eachkv(attr3(self, ?, ?), pairs);
78+
const entries = key;
79+
$.each(([key, value]) => attr3(self, key, value), entries);
8080
}
8181
}
8282

@@ -95,7 +95,7 @@ function attrN(self, ...kvps){
9595
}
9696
}
9797

98-
export const attr = _.overload(null, null, attr2, attr3, attrN);
98+
export const attr = _.overload(null, _.comp(_.into({}, ?), $.assert), attr2, attr3, attrN);
9999

100100
function removeAttr2(self, key){
101101
self.removeAttribute(key);

src/dom/types/element/behave.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,12 @@ function lookup(self, key){
134134
return self.getAttribute(key);
135135
}
136136

137-
function contains(self, key){
137+
function contains2(self, key){
138138
return self.hasAttribute(key);
139139
}
140140

141+
const contains = _.overload(null, _.constantly(_.looseEq), contains2);
142+
141143
function parent(self){
142144
return self && self.parentNode;
143145
}
@@ -200,8 +202,7 @@ function omit2(self, node){
200202
$.each(self.removeAttribute.bind(self), keys);
201203
} else if (isAttrs(node)) {
202204
const attrs = node;
203-
$.each(function(entry){
204-
const key = entry[0], value = entry[1];
205+
$.each(function([key, value]){
205206
let curr = lookup(self, key);
206207
if (_.isObject(curr)){
207208
curr = mapa(function(pair){
@@ -229,7 +230,7 @@ function includes(self, target){
229230
}, true, keys);
230231
} else if (isAttrs(target)) {
231232
return _.reducekv(function(memo, key, value){
232-
return memo ? lookup(self, key) == value : reduced(memo);
233+
return memo ? _.contains(self, key, value) : reduced(memo);
233234
}, true, target);
234235
} else {
235236
return _.detect(_.isString(target) ? function(node){
@@ -364,6 +365,7 @@ export default _.does(
364365
icontents,
365366
ievented,
366367
iselectable,
368+
_.implement($.ITopic, _.itopic(assoc, dissoc, {equals: _.looseEq})),
367369
_.keying("Element"),
368370
_.implement(_.IReducible, {reduce}),
369371
_.implement(IValue, {value}),

src/dom/types/url-search-params/behave.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,14 @@ function vals(self){
7979
return _.lazyIterable(self.values());
8080
}
8181

82-
function contains(self, key){
82+
function contains2(self, key){
8383
return self.has(key);
8484
}
8585

86+
const contains = _.overload(null, _.constantly(_.looseEq), contains2);
87+
8688
function includes(self, [key, value]) {
87-
return _.equiv(lookup(self, key), value);
89+
return _.contains(self, key, value);
8890
}
8991

9092
export default _.does(

src/shell/protocols.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export * from "./protocols/iresettable/instance.js";
1919
export * from "./protocols/isend/instance.js";
2020
export * from "./protocols/iqueryable/instance.js";
2121
export * from "./protocols/ilogger/instance.js";
22+
export * from "./protocols/itopic/instance.js";

src/shell/protocols/concrete.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export * from "./iresettable/concrete.js";
1919
export * from "./isend/concrete.js";
2020
export * from "./iqueryable/concrete.js";
2121
export * from "./ilogger/concrete.js";
22+
export * from "./itopic/concrete.js";

0 commit comments

Comments
 (0)