Skip to content

Commit

Permalink
Changed to inherit functional methods from Array.prototype.
Browse files Browse the repository at this point in the history
  • Loading branch information
henry-luo committed Dec 11, 2018
1 parent 2fe625c commit b6c85fe
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 80 deletions.
2 changes: 1 addition & 1 deletion dist/mark.core.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mark.es5.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mark.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ Note: /dist/mark.js has bundled all sub-modules and all dependencies with it, an
- [Mark conversion example](https://plnkr.co/edit/cMSCW3?p=preview)


## Extensions and Applications of Mark
## Tools, Extensions and Applications of Mark

- [Mark Template](https://github.com/henry-luo/mark-template): a JS template engine inspired by JSX and XSLT, using Mark for the template syntax.
- [Mark VSC Extension](https://marketplace.visualstudio.com/items?itemName=henryluo.mark-vsce): Mark Notation support for Visual Studio Code.

## Credits

Expand Down
92 changes: 16 additions & 76 deletions mark.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,92 +104,32 @@ var MARK = (function() {

// Mark object API functions
var api = {
// object 'properties': just use JS Object.keys(), Object.values(), Object.entries() to work with the properties
// object 'properties': just use JS Object.keys(), Object.values(), Object.entries() to work with the properties

// return the content items
contents: function() {
let list = [];
for (let c of this) { list.push(c); }
return list;
},
// get contents length
// length: function() {
// return this[$length];
// },

// get parent
parent: function(pa) {
return this[$parent];
},

// filter: like Array.prototype.filter
filter: function(func, thisArg) {
if (!(typeof func === 'function' && this)) throw new TypeError();
const obj = Object(this);
let res = [], i = 0;
for (const n of obj) {
if (func.call(thisArg || obj, n, i, obj)) { res.push(n); }
i++;
}
return res;
},

// map: like Array.prototype.map
map: function(func, thisArg) {
if (!(typeof func === 'function' && this)) throw new TypeError();
const obj = Object(this);
let res = [], i = 0;
for (const n of obj) {
res[i] = func.call(thisArg || obj, n, i, obj);
i++;
}
return res;
},

// reduce: like Array.prototype.reduce
reduce: function(func) {
if (!(typeof func === 'function' && this)) throw new TypeError();
let obj = Object(this), len = obj[$length], k = 0, value;
if (arguments.length == 2) { value = arguments[1]; }
else {
if (k >= len) { throw new TypeError('Reduce of empty contents with no initial value'); }
value = obj[k++];
}
for (; k < len; k++) {
value = func(value, obj[k], k, obj);
}
return value;
},

// every: like Array.prototype.every
every: function(func, thisArg) {
if (!(typeof func === 'function' && this)) throw new TypeError();
let i = 0, obj = Object(this);
for (const n of obj) {
var result = func.call(thisArg || obj, n, i, obj);
if (!result) { return false; }
i++;
}
return true;
},

// some: like Array.prototype.some
some: function(func, thisArg) {
if (!(typeof func === 'function' && this)) throw new TypeError();
let i = 0, obj = Object(this);
for (const n of obj) {
if (func.call(thisArg || obj, n, i, obj)) { return true; }
i++;
}
return false;
},

// each: like Array.prototype.forEach
each: function(func, thisArg) {
if (!(typeof func === 'function' && this)) throw new TypeError();
let i = 0, obj = Object(this);
for (const n of obj) {
func.call(thisArg || obj, n, i, obj);
i++;
}
},
// as Mark is array-like, we can simply inherit all functional methods from Array.prototype
filter: Array.prototype.filter,
map: Array.prototype.map,
reduce: Array.prototype.reduce,
every: Array.prototype.every,
some: Array.prototype.some,
each: Array.prototype.forEach,
forEach: Array.prototype.forEach,
includes: Array.prototype.includes,
indexOf: Array.prototype.indexOf,
lastIndexOf: Array.prototype.lastIndexOf,
slice: Array.prototype.slice,

// conversion APIs
source: function(options) {
Expand Down
12 changes: 12 additions & 0 deletions test/mark-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ test('Mark object model', function(assert) {
// each
let types = []; div.each(n => types.push(typeof n));
assert.deepEqual(types, ["string", "object", "string", "object", "object"], "Mark each API");
types = []; div.forEach(n => types.push(typeof n));
assert.deepEqual(types, ["string", "object", "string", "object", "object"], "Mark forEach API");
// includes
assert.equal(div.includes("more"), true, "Mark includes API");
assert.equal(div.includes("test"), false, "Mark includes API");
// indexOf
assert.equal(div.indexOf("more"), 2, "Mark indexOf API");
// lastIndexOf
assert.equal(div.lastIndexOf("more"), 2, "Mark lastIndexOf API");
// slice
let items = [div[1], "more"];
assert.deepEqual(div.slice(1,3), items, "Mark slice API");

// direct content assignment - not advisable
var div = Mark.parse('{div "text"}');
Expand Down

0 comments on commit b6c85fe

Please sign in to comment.