Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Very basic money filter #55

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.3.3-alpha / 2016-09-15
==================

* Tags `increment`, `decrement`
* Money filters `money`, `money_with_currency`, `money_without_trailing_zeros`, `money_without_currency`
* Math filters `ceil`, `floor`, `round`
* Fix range to support variable, eg. (start..end)

1.3.2 / 2015-04-27
==================

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ source 'http://rubygems.org'

gem 'sprockets', '~>1.0.2'
gem 'rake', '~> 10.4.2'
gem 'listen', '~> 3.1.5'
94 changes: 80 additions & 14 deletions dist/liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,22 @@ Liquid.Context = Liquid.Class.extend({
left = parseInt(range[1]),
right = parseInt(range[2]),
arr = [];
if (isNaN(left) || isNaN(right)) {
left = range[1].charCodeAt(0);
right = range[2].charCodeAt(0);

var limit = right-left+1;
for (var i=0; i<limit; i++) arr.push(String.fromCharCode(i+left));
} else { // okay to make array
var limit = right-left+1;
for (var i=0; i<limit; i++) arr.push(i+left);
if(isNaN(left)){
let varLeft = this.resolve(range[1]);
left = parseInt(varLeft);
if(isNaN(left)){
throw new Error('Incorrect param for range: ' + key);
}
}
if(isNaN(right)){
let varRight = this.resolve(range[2]);
right = parseInt(varRight);
if(isNaN(right)){
throw new Error('Incorrect param for range: ' + key);
}
}
var limit = right-left+1;
for (var i=0; i<limit; i++) arr.push(i+left);
return arr;
} else {
var result = this.variable(key);
Expand Down Expand Up @@ -1230,8 +1236,6 @@ Liquid.Template.registerTag( 'increment', Liquid.Tag.extend({

init: function(tagName, markup, tokens) {
var parts = markup.match(this.tagSyntax);
console.log(tagName, markup, tokens);
console.log(parts[1]);
if( parts ) {
this.name = parts[1];
} else {
Expand Down Expand Up @@ -1264,8 +1268,6 @@ Liquid.Template.registerTag( 'decrement', Liquid.Tag.extend({

init: function(tagName, markup, tokens) {
var parts = markup.match(this.tagSyntax);
console.log(tagName, markup, tokens);
console.log(parts[1]);
if( parts ) {
this.name = parts[1];
} else {
Expand All @@ -1292,6 +1294,39 @@ Liquid.Template.registerTag( 'decrement', Liquid.Tag.extend({
return output;
}
}));
(function(Liquid){

var Money = {
getCurrencySign: function(){
var currencySign = '$'; // TODO: configurable currency
return currencySign;
},
getCurrencyCode: function(){
var currencyCode = ''; // TODO: configurable currency
return currencyCode ? ' ' + currencyCode : '';
},
format: function(num, currencySign, trimZero){
var ret = '0.00';
try{
num = parseFloat(('' + num) || ret);
if(trimZero !== true){
num = Math.round(num * 100) / 100;
}
var tokens = ('' + num).match(/^(-?)(\d+)(.(\d+))?$/) || [];
var sign = tokens[1] || '';
var digit = tokens[2] || '0';
var dp = ((tokens[4] || '') + '00');
if(trimZero){
dp = dp.replace(/0+$/, '');
}else{
dp = dp.substr(0, 2);
}
ret = sign + currencySign + digit + (dp.length > 0 ? '.' + dp : '');
} catch(e){}
return ret;
}
};

Liquid.Template.registerFilter({

_HTML_ESCAPE_MAP: {
Expand Down Expand Up @@ -1444,6 +1479,19 @@ Liquid.Template.registerFilter({
return (Number(input) || 0) % (Number(number) || 0);
},

ceil: function(input) {
return Math.ceil(Number(input) || 0);
},

floor: function(input) {
return Math.floor(Number(input) || 0);
},

round: function(input, dp) {
let pow = Math.pow(10, dp || 0);
return Math.round((Number(input) || 0) * pow) / pow;
},

map: function(input, property) {
input = input || [];
var results = [];
Expand Down Expand Up @@ -1473,10 +1521,28 @@ Liquid.Template.registerFilter({

append: function(input, string) {
return '' + (input || '').toString() + (string || '').toString();
}
},

money: function(input){
return Money.format(input, Money.getCurrencySign(), false);
},

money_with_currency: function(input){
return Money.format(input, Money.getCurrencySign(), false) + Money.getCurrencyCode();
},

money_without_trailing_zeros: function(input){
return Money.format(input, Money.getCurrencySign(), true);
},

money_without_currency: function(input){
return Money.format(input, '', false);
},

});

})(Liquid);


if(!(new Date()).strftime) {(function(){
Date.ext={};Date.ext.util={};Date.ext.util.xPad=function(x,pad,r){if(typeof (r)=="undefined"){r=10}for(;parseInt(x,10)<r&&r>1;r/=10){x=pad.toString()+x}return x.toString()};Date.prototype.locale="en-GB";if(document.getElementsByTagName("html")&&document.getElementsByTagName("html")[0].lang){Date.prototype.locale=document.getElementsByTagName("html")[0].lang}Date.ext.locales={};Date.ext.locales.en={a:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],A:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],b:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],B:["January","February","March","April","May","June","July","August","September","October","November","December"],c:"%a %d %b %Y %T %Z",p:["AM","PM"],P:["am","pm"],x:"%d/%m/%y",X:"%T"};
Expand Down
Loading