-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jon Bretman
committed
Sep 27, 2015
1 parent
24fe88e
commit 42cbaac
Showing
11 changed files
with
478 additions
and
393 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"rules": { | ||
|
||
"camelcase": [2, {"properties": "never"}], | ||
"comma-style": [2, "last"], | ||
"no-inner-declarations": 2, | ||
"no-lonely-if": 2, | ||
"spaced-comment": [2, "always"], | ||
"indent": [2, 4], | ||
"no-func-assign": 2, | ||
"curly": 2, | ||
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}], | ||
"brace-style": [2, "1tbs"], | ||
"no-shadow": [2, {"hoist": "never"}], | ||
"one-var": [2, "never"], | ||
"space-in-brackets": 0, | ||
"key-spacing": 0, | ||
"no-inline-comments": 0, | ||
"max-nested-callbacks": [2, 2], | ||
"max-len": 0, | ||
"max-depth": [2, 3], | ||
"quotes": [2, "single", "avoid-escape"], | ||
"no-new": 0, | ||
"no-multi-spaces": 0, | ||
"space-infix-ops": 0, | ||
"comma-spacing": 0, | ||
"no-unused-vars": 0, | ||
"func-style": [2, "declaration"], | ||
"no-use-before-define": [2, "nofunc"], | ||
"semi": [2, "always"], | ||
"eqeqeq": [2, "smart"], | ||
|
||
// turned off | ||
"no-console": 0, | ||
"strict": 0, | ||
"no-underscore-dangle": 0, | ||
"consistent-return": 0 | ||
|
||
}, | ||
"env": { | ||
"browser": true | ||
} | ||
} |
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 |
---|---|---|
|
@@ -8,3 +8,5 @@ jinja_to_js.egg-info/ | |
.coverage | ||
.tox | ||
.idea | ||
.cache | ||
node_modules/ |
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,262 @@ | ||
(function (global, factory) { | ||
|
||
if (typeof define === 'function' && define.amd) { | ||
define(['exports'], factory); | ||
} else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') { | ||
factory(exports); | ||
} else { | ||
root.jinjaToJS = {}; | ||
factory(root.jinjaToJS); | ||
} | ||
|
||
}(this, function (exports) { | ||
|
||
function escaper(match) { | ||
return { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
"'": ''', | ||
'`': '`' | ||
}[match]; | ||
} | ||
|
||
var ESCAPE_TEST_REGEX = /(?:&|<|>|"|'|`)/; | ||
var ESCAPE_REPLACE_REGEX = new RegExp(ESCAPE_TEST_REGEX.source, 'g'); | ||
var OBJECT_TYPE_REGEX = /\[object (.*?)]/; | ||
|
||
exports.filters = { | ||
|
||
capitalize: function (s) { | ||
return s ? s[0].toUpperCase() + s.substring(1) : s; | ||
}, | ||
|
||
batch: function (arr, size, fillWith) { | ||
var batched = arr.reduce(function (result, value) { | ||
|
||
var curr = result[result.length - 1]; | ||
if (!curr || curr.length === size) { | ||
result.push([]); | ||
curr = result[result.length - 1]; | ||
} | ||
|
||
curr.push(value); | ||
return result; | ||
}, []); | ||
|
||
var last = batched[batched.length - 1]; | ||
if (last && last.length < size && fillWith !== undefined) { | ||
for (var i = 0; i < size - last.length; i++) { | ||
last.push(fillWith); | ||
} | ||
} | ||
|
||
return batched; | ||
}, | ||
|
||
'default': function (obj, defaultValue, boolean) { | ||
defaultValue = defaultValue === undefined ? '' : defaultValue; | ||
boolean = boolean === undefined ? false : boolean; | ||
|
||
var test; | ||
|
||
if (boolean === true) { | ||
if (!obj) { | ||
test = false; | ||
} else if (Array.isArray(obj)) { | ||
test = obj.length > 0; | ||
} else { | ||
try { | ||
var keys = Object.keys(obj); | ||
test = keys.length > 0; | ||
} catch (e) { | ||
test = !!obj; | ||
} | ||
} | ||
} else { | ||
test = obj !== undefined; | ||
} | ||
|
||
return test ? obj : defaultValue; | ||
}, | ||
|
||
int: function (value, defaultValue) { | ||
defaultValue = defaultValue === undefined ? 0 : defaultValue; | ||
value = parseInt(value, 10); | ||
return isNaN(value) ? defaultValue : value; | ||
}, | ||
|
||
slice: function (value, slices, fillWith) { | ||
var hasFillWith = fillWith != null; | ||
var length = value.length; | ||
var itemsPerSlice = Math.floor(length / slices); | ||
var slicesWithExtra = length % slices; | ||
var offset = 0; | ||
var result = []; | ||
|
||
for (var i = 0; i < slices; i++) { | ||
var start = offset + i * itemsPerSlice; | ||
|
||
if (i < slicesWithExtra) { | ||
offset += 1; | ||
} | ||
|
||
var end = offset + (i + 1) * itemsPerSlice; | ||
var tmp = value.slice(start, end); | ||
|
||
if (hasFillWith && i >= slicesWithExtra) { | ||
tmp.push(fillWith); | ||
} | ||
|
||
result.push(tmp); | ||
} | ||
|
||
return result; | ||
}, | ||
|
||
title: function (s) { | ||
s = s + ''; | ||
return s.split(' ').map(function (word) { | ||
return word[0].toUpperCase() + word.substring(1).toLowerCase(); | ||
}).join(' '); | ||
}, | ||
|
||
truncate: function (s, length, killwords, end) { | ||
s = s + ''; | ||
length = length === undefined ? 255 : length; | ||
killwords = killwords === undefined ? false : killwords; | ||
end = end === undefined ? '...' : end; | ||
|
||
var endLength = end.length; | ||
|
||
if (s.length <= length) { | ||
return s; | ||
} else if (killwords) { | ||
return s.substring(0, length - endLength) + end; | ||
} | ||
|
||
s = s.substring(0, length - endLength).split(' '); | ||
s.pop(); | ||
s = s.join(' '); | ||
if (s.length < length) { | ||
s += ' '; | ||
} | ||
return s + end; | ||
}, | ||
|
||
first: function (obj) { | ||
return Array.isArray(obj) ? obj[0] : null; | ||
}, | ||
|
||
last: function (obj) { | ||
return Array.isArray(obj) ? obj[obj.length - 1] : null; | ||
}, | ||
|
||
size: function (obj) { | ||
if (Array.isArray(obj)) { | ||
return obj.length; | ||
} | ||
try { | ||
var keys = Object.keys(obj); | ||
} catch (e) { | ||
return 0; | ||
} | ||
return keys.length; | ||
} | ||
|
||
}; | ||
|
||
var runtime = exports.runtime = { | ||
|
||
type: function (o) { | ||
return Object.prototype.toString.call(o).match(OBJECT_TYPE_REGEX)[1]; | ||
}, | ||
|
||
boolean: function (o) { | ||
if (!o) { | ||
return false; | ||
} | ||
if (o === true) { | ||
return o; | ||
} | ||
if (Array.isArray(o)) { | ||
return o.length > 0; | ||
} | ||
if (runtime.type(o) === 'Object') { | ||
return Object.keys(o).length > 0; | ||
} | ||
return !!o; | ||
}, | ||
|
||
each: function (obj, fn) { | ||
if (Array.isArray(obj)) { | ||
return obj.forEach(fn); | ||
} | ||
try { | ||
var keys = Object.keys(obj); | ||
} catch (e) { | ||
return; | ||
} | ||
keys.forEach(function (k) { | ||
fn(obj[k], k); | ||
}); | ||
}, | ||
|
||
isEqual: function (objA, objB) { | ||
var typeA; | ||
var keysA; | ||
var i; | ||
|
||
if (objA === objB) { | ||
return true; | ||
} | ||
|
||
typeA = runtime.type(objA); | ||
|
||
if (typeA !== runtime.type(objB)) { | ||
return false; | ||
} | ||
|
||
if (typeA === 'Array') { | ||
|
||
if (objA.length !== objB.length) { | ||
return false; | ||
} | ||
|
||
for (i = 0; i < objA.length; i++) { | ||
if (!runtime.isEqual(objA[i], objB[i])) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
if (runtime.type(objA) === 'Object') { | ||
keysA = Object.keys(objA); | ||
|
||
if (keysA.length !== Object.keys(objB).length) { | ||
return false; | ||
} | ||
|
||
for (i = 0; i < keysA.length; i++) { | ||
if (!runtime.isEqual(objA[keysA[i]], objB[keysA[i]])) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
}, | ||
|
||
escape: function (str) { | ||
str = str == null ? '' : '' + str; | ||
return ESCAPE_TEST_REGEX.test(str) ? str.replace(ESCAPE_REPLACE_REGEX, escaper) : str; | ||
} | ||
|
||
}; | ||
|
||
})); |
Oops, something went wrong.