forked from mozilla/nunjucks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Jinja2-style boolean tests, as requested in mozilla#532 and mozi…
…lla#1015. - adds 21 Jinja2-style boolean tests - updates node list, parser and compiler to interpret and properly compile `Is` nodes - adds tests for tests, parser and compiler changes/additions - no changes to existing tests - adds `addTest`/`getTest` methods to the environment - CAVEAT: changes lexer to interpret "null" as `null` instead of a lookup to "null" in the context. this was necessary to make the test `null is null` pass - CAVEAT: adds dependency on ES6 Symbols, Maps and Sets for `iterable` and `mapping` tests. These may need to be removed if we're not okay with dumping IE. The tests for these tests require ES6 features and would need to be removed as well.
- Loading branch information
Showing
9 changed files
with
353 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use strict'; | ||
|
||
var SafeString = require('./runtime').SafeString; | ||
|
||
exports.callable = function(value) { | ||
return typeof value === 'function'; | ||
}; | ||
|
||
exports.defined = function(value) { | ||
return value !== undefined; | ||
}; | ||
|
||
exports.divisibleby = function(one, two) { | ||
return (one % two) === 0; | ||
}; | ||
|
||
exports.escaped = function(value) { | ||
return value instanceof SafeString; | ||
}; | ||
|
||
exports.equalto = function(one, two) { | ||
return one === two; | ||
}; | ||
|
||
exports.even = function(value) { | ||
return value % 2 === 0; | ||
}; | ||
|
||
exports.falsy = function(value) { | ||
return !value; | ||
}; | ||
|
||
exports.greaterthan = function(one, two) { | ||
return one > two; | ||
}; | ||
|
||
exports.lessthan = function(one, two) { | ||
return one < two; | ||
}; | ||
|
||
exports.lower = function(value) { | ||
return value.toLowerCase() === value; | ||
}; | ||
|
||
exports.number = function(value) { | ||
return typeof value === 'number'; | ||
}; | ||
|
||
exports.none = function(value) { | ||
return value === null; | ||
}; | ||
|
||
exports.null = function(value) { | ||
return value === null; | ||
}; | ||
|
||
exports.odd = function(value) { | ||
return value % 2 === 1; | ||
}; | ||
|
||
exports.sameas = function(one, two) { | ||
return Object.is(one, two); | ||
}; | ||
|
||
exports.string = function(value) { | ||
return typeof value === 'string'; | ||
}; | ||
|
||
exports.truthy = function(value) { | ||
return !!value; | ||
}; | ||
|
||
exports.undefined = function(value) { | ||
return value === undefined; | ||
}; | ||
|
||
exports.upper = function(value) { | ||
return value.toUpperCase() === value; | ||
}; | ||
|
||
/** | ||
* ES6 features required. Unless we're okay with nuking IE support, these may | ||
* need to be removed. | ||
*/ | ||
|
||
exports.iterable = function(value) { | ||
if (Symbol) { | ||
return !!value[Symbol.iterator]; | ||
} else { | ||
throw new Error('ES6 Symbols are unavailable in your browser or runtime environment'); | ||
} | ||
}; | ||
|
||
exports.mapping = function(value) { | ||
// only maps and object hashes | ||
if (Set) { | ||
return value !== null | ||
&& value !== undefined | ||
&& typeof value === 'object' | ||
&& !Array.isArray(value) | ||
&& !(value instanceof Set); | ||
} else { | ||
throw new Error('ES6 Sets are unavailable in your browser or runtime environment.'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.