Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ry5n committed Mar 27, 2015
0 parents commit 7c2b113
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
bower_components
23 changes: 23 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt);

// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),

simplemocha: {
options: {
globals:['should'],
timeout: 3000,
ignoreLeaks: false,
ui: 'bdd',
reporter: 'tap'
},
all: {
src: ['test/**/*.js']
}
},
});

grunt.registerTask('test', ['simplemocha']);
};
56 changes: 56 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
(function(dust) {
var dust = require('dustjs-linkedin');
var stringify = require('json-stringify-pretty-compact');
require('dustjs-helpers');

dust.helpers.component = function component(chunk, context, bodies, params) {
if (!params.name) {
return chunk;
}

var originalContext = context;
var thisContext;
var template;
var args = [];

bodies = bodies || {};
params = params || {};

// Start with an empty context.
thisContext = dust.makeBase(context.global);

// Set the template to render.
template = thisContext.templateName = params.name;

// Handle params and bodies
//
// We evaluate these in the original context available to the component.
// To accomplish this, we need to do some extra work.
[bodies, params].forEach(function(obj, index) {
for (var prop in obj) {
var ret;

// If the body or param is itself a Dust body (rather than a literal),
// it is a function of the form fn(chunk, context). We wrap this
// function with a new function which is bound to the dust body. When
// it is evaluated, we make it use the original context available to the
// include rather than the immediate context passed in by the caller.
if (typeof obj[prop] === 'function') {
ret = function(chunk) {
return this(chunk, originalContext).data;
}.bind(obj[prop]);
} else {
ret = obj[prop];
}

args[prop] = ret;
}
});

// Push on the evaluated bodies and params.
thisContext = thisContext.push(args);

// Return the evaluated template.
return dust.load(template, chunk, thisContext);
}
})(typeof exports !== 'undefined' ? module.exports = require('dustjs-linkedin') : dust);
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "dust-component-helper",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ryan L Frederick <[email protected]> (http://ryanfrederick.com/)",
"license": "MIT",
"dependencies": {
"dustjs-helpers": "^1.6.1",
"dustjs-linkedin": "^2.6.1",
"grunt": "^0.4.5",
"grunt-simple-mocha": "^0.4.0",
"json-stringify-pretty-compact": "^1.0.1",
"load-grunt-tasks": "^3.1.0",
"should": "~1.2.2"
}
}
10 changes: 10 additions & 0 deletions test/fixtures/example.dust
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="c-example{?isFeatured} c--featured{/isFeatured}">
{body}
{other}

{! This should only print when the outer context is explicitly passed in. !}
{?outer}<p>{outer}</p>{/outer}

{! Only present when explicitly passed in !}
{?state.faved}<p>Faved!</p>{/state.faved}
</div>
51 changes: 51 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Declare deps.
var dust = require('dustjs-linkedin');
var assert = require('assert');
var fs = require('fs');
var stringify = require('json-stringify-pretty-compact');
require('dustjs-helpers');
require('../index');

// Set up, compile and load the example component.
var templateName = 'example';
var context = {
outer: 'Outer',
passed: 'Passed!',
example: 'example',
state: {
faved: true
}
};

dust.compileFn(fs.readFileSync('test/fixtures/example.dust', 'utf8'), templateName);

// Run the tests
describe('component helper', function() {
// Todo these two tests aren’t testing the helper: remove them
it('can access keys in the current context.', function() {
var code = '<p>{passed}</p>';

dust.renderSource(code, context, function(err, out) {
assert.equal(out, '<p>Passed!</p>');
});
});

it('can render a template.', function() {
dust.render(templateName, dust.makeBase({}), function(err, out) {
assert.equal(out, '<div class="c-example"></div>');
});
});

// Proper tests
it('exists.', function() {
assert.equal(typeof dust.helpers.component, 'function');
});

it('has strong encapsulation by default.', function() {
var code = '{@component name="example" /}';

dust.renderSource(code, context, function(err, out) {
assert.equal(out, '<div class="c-example"></div>');
});
});
});

0 comments on commit 7c2b113

Please sign in to comment.