diff --git a/src/Edge/index.js b/src/Edge/index.js index c496e9a..6807016 100644 --- a/src/Edge/index.js +++ b/src/Edge/index.js @@ -24,7 +24,7 @@ const ExistingTags = require('../Tags') class Edge { constructor () { this._tags = {} - this._globals = {} + this._globals = require('../Globals') this._loader = new Loader() this._boot() } diff --git a/src/Globals/index.js b/src/Globals/index.js new file mode 100644 index 0000000..aedd00f --- /dev/null +++ b/src/Globals/index.js @@ -0,0 +1,50 @@ +'use strict' + +/* + * edge + * + * (c) Harminder Virk + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. +*/ + +const _ = require('lodash') + +const range = function (start, end) { + return _.range(start, end) +} + +const batch = function (input, size) { + return _.chunk(input, size) +} + +const toJSON = function (input, indent = 2) { + return JSON.stringify(input, null, indent) +} + +const first = function (collection) { + return _.first(collection) +} + +const last = function (collection) { + return _.last(collection) +} + +const groupBy = function (collection, field) { + return _.groupBy(collection, field) +} + +const size = function (input) { + return _.size(input) +} + +module.exports = { + range, + batch, + toJSON, + first, + last, + groupBy, + size +} diff --git a/test/unit/edge.spec.js b/test/unit/edge.spec.js index 291907d..6ef4c04 100644 --- a/test/unit/edge.spec.js +++ b/test/unit/edge.spec.js @@ -14,6 +14,7 @@ const test = require('japa') const dedent = require('dedent-js') const Edge = require('../../src/Edge') const Presenter = require('../../src/Presenter') +const Globals = require('../../src/Globals') test.group('Edge', (group) => { group.before(() => { @@ -30,6 +31,11 @@ test.group('Edge', (group) => { assert.deepEqual(Object.keys(this.tags), Object.keys(edge._tags)) }) + test('should register all inbuilt globals', (assert) => { + const edge = new Edge() + assert.deepEqual(Object.keys(Globals), Object.keys(edge._globals)) + }) + test('throw exception when a tag does not have a tagName', (assert) => { const edge = new Edge() class DummyTag {} @@ -268,4 +274,10 @@ test.group('Edge', (group) => { assert.equal(output.trim(), `virk nikk`) }) + + test('should have access to inbuilt globals', (assert) => { + const edge = new Edge() + const statement = `{{ size('foo') }}` + assert.equal(edge.renderString(statement).trim(), '3') + }) })