Skip to content

Commit 606e293

Browse files
committed
rename attributes to add_attributes for clarity! #44
1 parent 25cd493 commit 606e293

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

Diff for: examples/todo-list/elmish.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
2-
3-
4-
51
/**
62
* `empty` the contents of a given DOM element "node" (before re-rendering).
73
* This is the *fastest* way according to: stackoverflow.com/a/3955238/1148249
@@ -45,9 +41,9 @@ function mount(model, update, view, root_element_id) {
4541
* @param {Object} node DOM node upon which attribute(s) should be applied
4642
* @example
4743
* // returns node with attributes applied
48-
* div = attributes(["class=item", "id=mydiv", "active=true"], div);
44+
* div = add_attributes(["class=item", "id=mydiv", "active=true"], div);
4945
*/
50-
function attributes(attrlist, node) {
46+
function add_attributes(attrlist, node) {
5147
attrlist.forEach(function (attr) {
5248
var a = attr.split('=');
5349
switch(a[0]) {
@@ -80,7 +76,7 @@ function init(doc){
8076
if (typeof module !== 'undefined' && module.exports) {
8177
module.exports = {
8278
// view: view,
83-
attributes: attributes,
79+
add_attributes: add_attributes,
8480
empty: empty,
8581
init: init,
8682
mount: mount

Diff for: test/elmish.test.js

+32-6
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,23 @@ test('elmish.mount app expect state to be Zero', function (t) {
5151
});
5252

5353

54-
test('elmish.attributes applies class HTML attribute to a node', function (t) {
54+
test('elmish.add_attributes applies HTML claa attribute to node', function (t) {
5555
const root = document.getElementById(id);
5656
let div = document.createElement('div');
5757
div.id = 'divid';
58-
div = elmish.attributes(["class=apptastic"], div);
58+
div = elmish.add_attributes(["class=apptastic"], div);
5959
root.appendChild(div);
6060
// test the div has the desired class:
6161
const nodes = document.getElementsByClassName('apptastic');
6262
t.equal(nodes.length, 1, "<div> has 'apptastic' class applied");
6363
t.end();
6464
});
6565

66-
test('elmish.attributes applies id HTML attribute to a node', function (t) {
66+
test('elmish.add_attributes applies id HTML attribute to a node', function (t) {
6767
const root = document.getElementById(id);
6868
elmish.empty(root);
6969
let el = document.createElement('section');
70-
el = elmish.attributes(["id=myid"], el);
70+
el = elmish.add_attributes(["id=myid"], el);
7171
const text = 'hello world!'
7272
var txt = document.createTextNode(text);
7373
el.appendChild(txt);
@@ -77,13 +77,39 @@ test('elmish.attributes applies id HTML attribute to a node', function (t) {
7777
t.end();
7878
});
7979

80-
test('test default branch of elmish.attributes (no effect)', function (t) {
80+
test('elmish.add_attributes applies multiple attribute to node', function (t) {
81+
const root = document.getElementById(id);
82+
elmish.empty(root);
83+
let el = document.createElement('span');
84+
el = elmish.add_attributes(["id=myid", "class=totes mcawesome"], el);
85+
const text = 'hello world'
86+
var txt = document.createTextNode(text);
87+
el.appendChild(txt);
88+
root.appendChild(el);
89+
const actual = document.getElementById('myid').textContent;
90+
t.equal(actual, text, "<section> has 'myid' id attribute");
91+
t.equal(el.className, 'totes mcawesome', "CSS class applied: ", el.className);
92+
t.end();
93+
});
94+
95+
test('test default branch of elmish.add_attributes (no effect)', function (t) {
96+
const root = document.getElementById(id);
97+
let div = document.createElement('div');
98+
div.id = 'divid';
99+
// "Clone" the div DOM node before invoking the elmish.attributes
100+
const clone = div.cloneNode(true);
101+
div = elmish.add_attributes(["unrecognised_attribute=noise"], div);
102+
t.deepEqual(div, clone, "<div> has not been altered");
103+
t.end();
104+
});
105+
106+
test('elmish.', function (t) {
81107
const root = document.getElementById(id);
82108
let div = document.createElement('div');
83109
div.id = 'divid';
84110
// "Clone" the div DOM node before invoking the elmish.attributes
85111
const clone = div.cloneNode(true);
86-
div = elmish.attributes(["unrecognised_attribute=noise"], div);
112+
div = elmish.add_attributes(["unrecognised_attribute=noise"], div);
87113
t.deepEqual(div, clone, "<div> has not been altered");
88114
t.end();
89115
});

Diff for: todo-list.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -439,19 +439,19 @@ Demo: https://ellie-app.com/LTtNVQjfWVa1
439439
![ellie-elm-section](https://user-images.githubusercontent.com/194400/42708957-bbcc1020-86d6-11e8-97bf-f2f3a1c6fdea.png)
440440

441441

442-
### `attributes`
442+
### `add_attributes`
443443

444-
The `JSDOC` comment for our `attributes` function is:
444+
The `JSDOC` comment for our `add_attributes` function is:
445445
```js
446446
/**
447-
* attributes applies the desired attributes to the desired node.
447+
* add_attributes applies the desired attributes to the desired node.
448448
* Note: this function is "impure" because it "mutates" the node.
449449
* however it is idempotent; the "side effect" is only applied once.
450450
* @param {Array.<String>} attrlist list of attributes to be applied to the node
451451
* @param {Object} node DOM node upon which attribute(s) should be applied
452452
* @example
453453
* // returns node with attributes applied
454-
* div = attributes(["class=item", "id=mydiv", "active=true"], div);
454+
* div = add_attributes(["class=item", "id=mydiv", "active=true"], div);
455455
*/
456456
```
457457
This should give you a _good idea_ of what code needs to be written.
@@ -460,11 +460,11 @@ But let's write the _test_ first!
460460
Add the following test to the `test/elmish.test.js` file: <br />
461461

462462
```js
463-
test('elmish.attributes applies class HTML attribute to a node', function (t) {
463+
test('elmish.add_attributes applies class HTML attribute to a node', function (t) {
464464
const root = document.getElementById(id);
465465
let div = document.createElement('div');
466466
div.id = 'divid';
467-
div = elmish.attributes(["class=apptastic"], div);
467+
div = elmish.add_attributes(["class=apptastic"], div);
468468
root.appendChild(div);
469469
// test the div has the desired class:
470470
const nodes = document.getElementsByClassName('apptastic');

0 commit comments

Comments
 (0)