Skip to content

Commit

Permalink
feat(globals): add couple of globals
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 21, 2017
1 parent b966077 commit f1bf6e0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Edge/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
50 changes: 50 additions & 0 deletions src/Globals/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

/*
* edge
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* 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
}
12 changes: 12 additions & 0 deletions test/unit/edge.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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 {}
Expand Down Expand Up @@ -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')
})
})

0 comments on commit f1bf6e0

Please sign in to comment.