Skip to content

Commit

Permalink
Move JS into a runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Bretman committed Sep 27, 2015
1 parent 24fe88e commit 42cbaac
Show file tree
Hide file tree
Showing 11 changed files with 478 additions and 393 deletions.
43 changes: 43 additions & 0 deletions .eslintrc
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
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ jinja_to_js.egg-info/
.coverage
.tox
.idea
.cache
node_modules/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ install:
- "virtualenv ~/.venv"
- "source ~/.venv/bin/activate"
- "pip install tox"
- "npm install && npm run-script lint"

script:
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then tox -e py26,pep8; fi
Expand Down
262 changes: 262 additions & 0 deletions jinja-to-js-runtime.js
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 {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&#34;',
"'": '&#x27;',
'`': '&#x60;'
}[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;
}

};

}));
Loading

0 comments on commit 42cbaac

Please sign in to comment.