mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-02 16:45:14 -07:00
feature: if the provided date is 'now', create a date (#14)
This commit is contained in:
+6
-4
@@ -1,5 +1,5 @@
|
|||||||
//const strftime = require('strftime').timezone(-(new Date()).getTimezoneOffset());
|
|
||||||
const strftime = require('./src/strftime.js');
|
const strftime = require('./src/strftime.js');
|
||||||
|
const _ = require('./src/util/underscore.js');
|
||||||
|
|
||||||
module.exports = function(liquid) {
|
module.exports = function(liquid) {
|
||||||
liquid.registerFilter('abs', v => Math.abs(v));
|
liquid.registerFilter('abs', v => Math.abs(v));
|
||||||
@@ -8,8 +8,10 @@ module.exports = function(liquid) {
|
|||||||
stringify(str).charAt(0).toUpperCase() + str.slice(1));
|
stringify(str).charAt(0).toUpperCase() + str.slice(1));
|
||||||
liquid.registerFilter('ceil', v => Math.ceil(v));
|
liquid.registerFilter('ceil', v => Math.ceil(v));
|
||||||
|
|
||||||
//liquid.registerFilter('date', (v, arg) => strftime(arg, v));
|
liquid.registerFilter('date', (v, arg) => {
|
||||||
liquid.registerFilter('date', (v, arg) => strftime(v, arg));
|
if (v === 'now') v = new Date();
|
||||||
|
return strftime(v, arg);
|
||||||
|
});
|
||||||
|
|
||||||
liquid.registerFilter('default', (v, arg) => arg || v);
|
liquid.registerFilter('default', (v, arg) => arg || v);
|
||||||
liquid.registerFilter('divided_by', (v, arg) => Math.floor(v / arg));
|
liquid.registerFilter('divided_by', (v, arg) => Math.floor(v / arg));
|
||||||
@@ -108,7 +110,7 @@ function getMaxFixed(l, r) {
|
|||||||
return Math.max(getFixed(l), getFixed(r));
|
return Math.max(getFixed(l), getFixed(r));
|
||||||
}
|
}
|
||||||
|
|
||||||
function stringify(obj){
|
function stringify(obj) {
|
||||||
obj = obj || "";
|
obj = obj || "";
|
||||||
return obj + '';
|
return obj + '';
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-2
@@ -1,3 +1,5 @@
|
|||||||
|
const _ = require('./util/underscore.js');
|
||||||
|
|
||||||
var Scope = {
|
var Scope = {
|
||||||
safeGet: function(str) {
|
safeGet: function(str) {
|
||||||
var i;
|
var i;
|
||||||
@@ -41,7 +43,7 @@ var Scope = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function setPropertyByPath(obj, path, val) {
|
function setPropertyByPath(obj, path, val) {
|
||||||
if (path instanceof String || typeof path === 'string') {
|
if (_.isString(path)) {
|
||||||
var paths = path.replace(/\[/g, '.').replace(/\]/g, '').split('.');
|
var paths = path.replace(/\[/g, '.').replace(/\]/g, '').split('.');
|
||||||
for (var i = 0; i < paths.length; i++) {
|
for (var i = 0; i < paths.length; i++) {
|
||||||
var key = paths[i];
|
var key = paths[i];
|
||||||
@@ -58,7 +60,7 @@ function setPropertyByPath(obj, path, val) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getPropertyByPath(obj, path) {
|
function getPropertyByPath(obj, path) {
|
||||||
if (path instanceof String || typeof path === 'string') {
|
if (_.isString(path)) {
|
||||||
var paths = path.replace(/\[/g, '.').replace(/\]/g, '').split('.');
|
var paths = path.replace(/\[/g, '.').replace(/\]/g, '').split('.');
|
||||||
paths.forEach(p => obj = obj && obj[p]);
|
paths.forEach(p => obj = obj && obj[p]);
|
||||||
return obj;
|
return obj;
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
function isString(value){
|
||||||
|
return value instanceof String || typeof value === 'string';
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.isString = isString;
|
||||||
+14
-6
@@ -1,8 +1,9 @@
|
|||||||
const chai = require("chai");
|
const chai = require("chai");
|
||||||
const chaiAsPromised = require("chai-as-promised");
|
const expect = chai.expect;
|
||||||
|
chai.use(require("chai-as-promised"));
|
||||||
|
|
||||||
var liquid = require('..')(),
|
var liquid = require('..')(),
|
||||||
ctx;
|
ctx;
|
||||||
chai.use(chaiAsPromised);
|
|
||||||
|
|
||||||
function test(src, dst) {
|
function test(src, dst) {
|
||||||
ctx = {
|
ctx = {
|
||||||
@@ -19,7 +20,7 @@ function test(src, dst) {
|
|||||||
category: 'bar'
|
category: 'bar'
|
||||||
}]
|
}]
|
||||||
};
|
};
|
||||||
return liquid.parseAndRender(src, ctx).should.eventually.equal(dst);
|
return expect(liquid.parseAndRender(src, ctx)).to.eventually.equal(dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('filters', function() {
|
describe('filters', function() {
|
||||||
@@ -36,9 +37,16 @@ describe('filters', function() {
|
|||||||
it('should support ceil 3', () => test('{{ "3.5" | ceil }}', '4'));
|
it('should support ceil 3', () => test('{{ "3.5" | ceil }}', '4'));
|
||||||
it('should support ceil 4', () => test('{{ 183.357 | ceil }}', '184'));
|
it('should support ceil 4', () => test('{{ 183.357 | ceil }}', '184'));
|
||||||
|
|
||||||
it('should support date: %a %b %d %Y', function() {
|
describe('date', function(){
|
||||||
var str = ctx.date.toDateString();
|
it('should support %a %b %d %Y', function() {
|
||||||
return test('{{ date | date:"%a %b %d %Y"}}', str);
|
var str = ctx.date.toDateString();
|
||||||
|
return test('{{ date | date:"%a %b %d %Y"}}', str);
|
||||||
|
});
|
||||||
|
it('should support "now"', function() {
|
||||||
|
var year = (new Date()).getFullYear();
|
||||||
|
var src = '{{ "now" | date: "%Y"}}';
|
||||||
|
return expect(liquid.parseAndRender(src)).to.eventually.match(/\d{4}/);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support default', () => test('{{false |default: "a"}}', 'a'));
|
it('should support default', () => test('{{false |default: "a"}}', 'a'));
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
const chai = require("chai");
|
||||||
|
const expect = chai.expect;
|
||||||
|
const _ = require('../../src/util/underscore.js');
|
||||||
|
|
||||||
|
describe('util/underscore', function() {
|
||||||
|
describe('.isString()', function(){
|
||||||
|
it('should return true for literal string', function(){
|
||||||
|
expect(_.isString('foo')).to.be.true;
|
||||||
|
});
|
||||||
|
it('should return true String instance', function(){
|
||||||
|
expect(_.isString(new String('foo'))).to.be.true;
|
||||||
|
});
|
||||||
|
it('should return false for 123 ', function(){
|
||||||
|
expect(_.isString(123)).to.be.false;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user