Skip to content

Commit

Permalink
Merge pull request #95 from jairodemorais/remove_helpers
Browse files Browse the repository at this point in the history
helpers removed from Dust core. New npm package created for helpers.
  • Loading branch information
jleppert committed Jul 23, 2012
2 parents 446ad26 + aee4f6e commit eb0c016
Show file tree
Hide file tree
Showing 23 changed files with 1,314 additions and 307 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ test:
#
jasmine:
node test/jasmine-test/server/specRunner.js

#
# Run code coverage and generate report
#
Expand Down Expand Up @@ -44,6 +43,7 @@ CORE = dist/dust-core-${VERSION}.js
CORE_MIN = dist/dust-core-${VERSION}.min.js
FULL = dist/dust-full-${VERSION}.js
FULL_MIN = dist/dust-full-${VERSION}.min.js
HELPERS = dustjs-helpers/lib

define HEADER
//
Expand All @@ -58,17 +58,19 @@ endef

export HEADER

#TODO: REMOVE THE HELPERS IN THE NEXT RELEASE
dust:
@@mkdir -p dist
@@touch ${CORE}
@@echo "$$HEADER" > ${CORE}
@@cat ${SRC}/dust.js\
${SRC}/dust-helpers.js >> ${CORE}
${HELPERS}/dust-helpers.js >> ${CORE}
@@echo ${CORE} built

@@touch ${FULL}
@@echo "$$HEADER" > ${FULL}
@@cat ${SRC}/dust.js\
${SRC}/dust-helpers.js\
${HELPERS}/dust-helpers.js\
${SRC}/compiler.js\
${SRC}/parser.js >> ${FULL}
@@echo ${FULL} built
Expand Down
39 changes: 29 additions & 10 deletions dist/dust-core-1.0.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ dust.escapeJs = function(s) {
})(dust);

if (typeof exports !== "undefined") {
dust.helpers = require("./dust-helpers").helpers;
//TODO: Remove the helpers from dust core in the next release.
dust.helpers = require("../dust-helpers/lib/dust-helpers").helpers;
if (typeof process !== "undefined") {
require('./server')(dust);
}
Expand All @@ -574,11 +575,12 @@ function isSelect(context) {

function filter(chunk, context, bodies, params, filter) {
var params = params || {},
actual, expected;
actual,
expected;
if (params.key) {
actual = context.get(params.key);
actual = helpers.tap(params.key, chunk, context);
} else if (isSelect(context)) {
actual = context.current().value;
actual = context.current().selectKey;
if (context.current().isResolved) {
filter = function() { return false; };
}
Expand Down Expand Up @@ -624,6 +626,7 @@ var helpers = {
idx: function(chunk, context, bodies) {
return bodies.block(chunk, context.push(context.stack.index));
},

contextDump: function(chunk, context, bodies) {
_console.log(JSON.stringify(context.stack));
return chunk;
Expand All @@ -647,6 +650,14 @@ var helpers = {
return output;
},

/**
if helper
@param cond, either a string literal value or a dust reference
a string literal value, is enclosed in double quotes, e.g. cond="2>3"
a dust reference is also enclosed in double quotes, e.g. cond="'{val}'' > 3"
cond argument should evaluate to a valid javascript expression
**/

"if": function( chunk, context, bodies, params ){
if( params && params.cond ){
var cond = params.cond;
Expand All @@ -661,19 +672,27 @@ var helpers = {
}
// no condition
else {
_console.log( "NO condition given in the if helper!" );
_console.log( "No condition given in the if helper!" );
}
return chunk;
},

/**
select/eq/lt/lte/gt/gte/default helper
@param key, either a string literal value or a dust reference
a string literal value, is enclosed in double quotes, e.g. key="foo"
a dust reference may or may not be enclosed in double quotes, e.g. key="{val}" and key=val are both valid
@param type (optiona), supported types are number, boolean, string, date, context, defaults to string
**/
select: function(chunk, context, bodies, params) {
if( params && params.key){
var key = params.key;
key = this.tap(key, chunk, context);
return chunk.render(bodies.block, context.push({ isSelect: true, isResolved: false, value: context.get(key) }));
// returns given input as output, if the input is not a dust reference, else does a context lookup
var key = this.tap(params.key, chunk, context);
return chunk.render(bodies.block, context.push({ isSelect: true, isResolved: false, selectKey: key }));
}
// no key
else {
_console.log( "No key given for the select tag!" );
_console.log( "No key given in the select helper!" );
}
return chunk;
},
Expand All @@ -698,7 +717,7 @@ var helpers = {
return filter(chunk, context, bodies, params, function(expected, actual) { return actual >= expected; });
},

"else": function(chunk, context, bodies, params) {
"default": function(chunk, context, bodies, params) {
return filter(chunk, context, bodies, params, function(expected, actual) { return true; });
}
};
Expand Down
39 changes: 29 additions & 10 deletions dist/dust-full-1.0.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ dust.escapeJs = function(s) {
})(dust);

if (typeof exports !== "undefined") {
dust.helpers = require("./dust-helpers").helpers;
//TODO: Remove the helpers from dust core in the next release.
dust.helpers = require("../dust-helpers/lib/dust-helpers").helpers;
if (typeof process !== "undefined") {
require('./server')(dust);
}
Expand All @@ -574,11 +575,12 @@ function isSelect(context) {

function filter(chunk, context, bodies, params, filter) {
var params = params || {},
actual, expected;
actual,
expected;
if (params.key) {
actual = context.get(params.key);
actual = helpers.tap(params.key, chunk, context);
} else if (isSelect(context)) {
actual = context.current().value;
actual = context.current().selectKey;
if (context.current().isResolved) {
filter = function() { return false; };
}
Expand Down Expand Up @@ -624,6 +626,7 @@ var helpers = {
idx: function(chunk, context, bodies) {
return bodies.block(chunk, context.push(context.stack.index));
},

contextDump: function(chunk, context, bodies) {
_console.log(JSON.stringify(context.stack));
return chunk;
Expand All @@ -647,6 +650,14 @@ var helpers = {
return output;
},

/**
if helper
@param cond, either a string literal value or a dust reference
a string literal value, is enclosed in double quotes, e.g. cond="2>3"
a dust reference is also enclosed in double quotes, e.g. cond="'{val}'' > 3"
cond argument should evaluate to a valid javascript expression
**/

"if": function( chunk, context, bodies, params ){
if( params && params.cond ){
var cond = params.cond;
Expand All @@ -661,19 +672,27 @@ var helpers = {
}
// no condition
else {
_console.log( "NO condition given in the if helper!" );
_console.log( "No condition given in the if helper!" );
}
return chunk;
},

/**
select/eq/lt/lte/gt/gte/default helper
@param key, either a string literal value or a dust reference
a string literal value, is enclosed in double quotes, e.g. key="foo"
a dust reference may or may not be enclosed in double quotes, e.g. key="{val}" and key=val are both valid
@param type (optiona), supported types are number, boolean, string, date, context, defaults to string
**/
select: function(chunk, context, bodies, params) {
if( params && params.key){
var key = params.key;
key = this.tap(key, chunk, context);
return chunk.render(bodies.block, context.push({ isSelect: true, isResolved: false, value: context.get(key) }));
// returns given input as output, if the input is not a dust reference, else does a context lookup
var key = this.tap(params.key, chunk, context);
return chunk.render(bodies.block, context.push({ isSelect: true, isResolved: false, selectKey: key }));
}
// no key
else {
_console.log( "No key given for the select tag!" );
_console.log( "No key given in the select helper!" );
}
return chunk;
},
Expand All @@ -698,7 +717,7 @@ var helpers = {
return filter(chunk, context, bodies, params, function(expected, actual) { return actual >= expected; });
},

"else": function(chunk, context, bodies, params) {
"default": function(chunk, context, bodies, params) {
return filter(chunk, context, bodies, params, function(expected, actual) { return true; });
}
};
Expand Down
2 changes: 1 addition & 1 deletion docs/index.dust.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<script src="lib/dust.js"></script>
<script src="lib/parser.js"></script>
<script src="lib/compiler.js"></script>
<script src="test/examples.js"></script>
<script src="test/jasmine-test/spec/grammarTests.js"></script>
<script src="test/uutest.js"></script>
<script src="test/core.js"></script>
{#tmpl names="select"/}
Expand Down
62 changes: 62 additions & 0 deletions dustjs-helpers/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#
# Run all tests
#
test:
node test/server.js

#
# Run jasmine-test
#
jasmine:
node test/jasmine-test/server/specRunner.js helpersTests

#
# Run code coverage and generate report
#
coverage:
cover run test/server.js && cover report && cover report html

#
# Build the docs
#
docs:
node docs/build.js

#
# Build dust.js
#

VERSION = ${shell cat package.json | grep version | grep -o '[0-9]\.[0-9]\.[0-9]\+'}


SRC = lib
VERSION = ${shell cat package.json | grep version | grep -o '[0-9]\.[0-9]\.[0-9]\+'}
HELPERS = dist/dust-helpers-${VERSION}.js


define HEADER
//
// Dust-helpers - Addional functionality for dustjs-linkedin package v${VERSION}
//
// Copyright (c) 2012, LinkedIn
// Released under the MIT License.
//

endef

export HEADER

helpers:
@@mkdir -p dist
@@touch ${HELPERS}
@@echo "$$HEADER" > ${HELPERS}
@@cat ${SRC}/dust-helpers.js >> ${HELPERS}
@@echo ${HELPERS} built

release: clean docs min
git add dist/*
git commit -a -m "release v${VERSION}"
git tag -a -m "version v${VERSION}" v${VERSION}
npm publish

.PHONY: test docs bench parser
Loading

0 comments on commit eb0c016

Please sign in to comment.