UNPKG

@extjs/sencha-cmd-linux-32

Version:

Productivity and performance optimization tool for building applications with Sencha Ext JS and Sencha Touch.

185 lines (154 loc) 6.18 kB
"use strict"; var Fashion = require('../export/Base.js'); var Numeric = require('../export/type/Numeric.js'); var List = require('../export/type/List.js'); module.exports = { init(runtime) { runtime.register({ length: function (list) { if (list.type !== 'list') { return new Numeric(arguments.length); } return new Numeric(list.items.length); }, nth: function (list, index) { if (list.type !== 'list') { list = new List([list]); } if (index.type != 'number' || index.value.toFixed(0) != index.value) { Fashion.raise('List index ' + index + ' must be an integer for \'nth\''); } var value = index.value; if (value < 0) { value = Math.max(1, list.items.length + value + 1); } if (value === 0) { Fashion.raise('List index ' + value + ' must be greater than or equal to 1 for \'nth\''); } if (value - 1 >= list.items.length) { Fashion.raise('List index is ' + value + ' but list is only ' + list.items.length + ' item' + (list.items.length === 1 ? '' : 's') + ' long for \'nth\''); } return list.get(value); }, first_value_of: function (list) { if (list.type !== 'list') { list = new List([list]); } return this.nth(list, new Numeric(1)); }, last_value_of: function (list) { if (list.type !== 'list') { list = new List(list); } return this.nth(list, new Numeric(list.items.length)); }, compact: function () { var list = arguments, items, sep = ', '; if (list.type !== 'list') { list = new List(list); } items = list.items; if (items.length == 1 && items[0].type == 'list') { list = items[0]; items = list.items; sep = list.separator; } list = new List(null, sep); for (var i = 0; i < items.length; i++) { var item = items[i]; if (this.unbox(item)) { list.add(item); } } return list; }, _compass_list_size: function () { var list = arguments; if (list.type !== 'list') { list = new List(list); } return new Numeric(list.items.length); }, join: function (list1, list2, separator) { if (list1.type !== 'list') { list1 = new List([list1]); list1.separator = null; } if (list2.type !== 'list') { list2 = new List([list2]); list2.separator = null; } if (!separator) { separator = (list1.items.length && list1.separator) || (list2.items.length && list2.separator) || ' '; } if (separator.type === 'literal') { switch (separator.value) { case 'comma': separator = ', '; break; case 'space': separator = ' '; break; case 'auto': separator = list1.separator || list2.separator || ' '; break; default: Fashion.raise('Separator name must be space, comma, or auto for \'join\''); break; } } if (separator.type === 'string') { separator = separator.value; } return new List(list1.items.concat(list2.items), separator); }, append: function () { return this.join.apply(this, arguments); }, box: function (list, index) { if (!(list instanceof List)) { list = new List([list]); } list = list.items.slice(); if (index >= list.length) { switch (list.length) { case 1: list[1] = list[2] = list[3] = list[0]; break; case 2: list[2] = list[0]; list[3] = list[1]; break; case 3: list[3] = list[1]; break; } } return list[index - 1]; }, zip: function () { var lists = this.sliceArgs(arguments), output = [], minLen = -1, list; for (var i = 0; i < lists.length; i++) { list = lists[i].items; if (minLen === -1) { minLen = list.length; } else if (list.length < minLen) { minLen = list.length; } } for (var i = 0; i < minLen; i++) { var newList = []; for (var j = 0; j < lists.length; j++) { newList.push(lists[j].items[i]); } output.push(new List(newList, ' ')); } return new List(output, ', '); } }); } }