forked from 0x2620/oxjs
less obscure Ox.map
This commit is contained in:
parent
33390069b9
commit
12cf77cef5
21 changed files with 125 additions and 101 deletions
|
|
@ -111,6 +111,7 @@ Ox.filter <f> Filters a collection by a given condition
|
|||
@*/
|
||||
Ox.filter = function(col, fn, that) {
|
||||
var ret, type = Ox.typeOf(col);
|
||||
fn = fn || Ox.identity;
|
||||
if (type == 'object') {
|
||||
ret = {};
|
||||
Ox.forEach(col, function(val, key) {
|
||||
|
|
@ -303,6 +304,16 @@ Ox.indexOf = function(col, fn) {
|
|||
return index == col.length ? -1 : index;
|
||||
};
|
||||
|
||||
// FIXME: use this instead of `Ox.filter(Ox.map())` when it's just about getting
|
||||
// the original indices
|
||||
Ox.indicesOf = function(col, fn) {
|
||||
return Ox.map(col, function(val, i) {
|
||||
return fn(val) ? i : null;
|
||||
}).filter(function(index) {
|
||||
return index !== null;
|
||||
});
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.isEmpty <f> Tests if a value is an empty array, object or string
|
||||
(value) -> <b> True if the value is an empty array, object or string
|
||||
|
|
@ -460,8 +471,7 @@ Ox.makeObject = function(arr) {
|
|||
/*@
|
||||
Ox.map <f> Transforms the values of an array, object or string
|
||||
Unlike <code>[].map()</code>, <code>Ox.map()</code> works for arrays,
|
||||
objects and strings. Returning <code>null</code> from the iterator
|
||||
function will remove the element from the collection.
|
||||
objects and strings.
|
||||
> Ox.map([0, 0, 0], function(v, i) { return v == i; })
|
||||
[true, false, false]
|
||||
> Ox.map({a: 'a', b: 'a', c: 'a'}, function(v, k) { return v == k; })
|
||||
|
|
@ -474,21 +484,19 @@ Ox.map <f> Transforms the values of an array, object or string
|
|||
# > Ox.map([,], function(v, i) { return i; })
|
||||
# [0]
|
||||
@*/
|
||||
|
||||
// FIXME: it would sometimes be nice to have Ox.map(3, function(i) {...})
|
||||
// instead of Ox.range(3).map(function(i) {...})
|
||||
|
||||
Ox.map = function(obj, fn) {
|
||||
// fixme: return null to filter out may be a bit esoteric
|
||||
var isObject = Ox.isObject(obj),
|
||||
ret = isObject ? {} : [];
|
||||
Ox.forEach(obj, function(val, key) {
|
||||
// FIXME: is there any reason for this strange assignment?
|
||||
var map;
|
||||
if ((map = fn(val, key)) !== null) {
|
||||
ret[isObject ? key : ret.length] = map;
|
||||
Ox.map = function(col, fn, that) {
|
||||
var type = Ox.typeOf(col), ret;
|
||||
if (type == 'object') {
|
||||
ret = {};
|
||||
Ox.forEach(col, function(val, key) {
|
||||
ret[key] = fn.call(that, val, key, col);
|
||||
});
|
||||
} else {
|
||||
ret = Ox.toArray(col).map(fn);
|
||||
if (type == 'string') {
|
||||
ret = ret.join('');
|
||||
}
|
||||
});
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -25,9 +25,9 @@ Ox.getDateInWeek = function(date, weekday, utc) {
|
|||
date = Ox.makeDate(date);
|
||||
var sourceWeekday = Ox.getISODay(date, utc),
|
||||
targetWeekday = Ox.isNumber(weekday) ? weekday
|
||||
: Ox.map(Ox.WEEKDAYS, function(v, i) {
|
||||
return v.substr(0, 3) == weekday.substr(0, 3) ? i + 1 : null;
|
||||
})[0];
|
||||
: Ox.indexOf(Ox.WEEKDAYS, function(v) {
|
||||
return v.substr(0, 3) == weekday.substr(0, 3);
|
||||
}) + 1;
|
||||
return Ox.setDate(date, Ox.getDate(date, utc) - sourceWeekday + targetWeekday, utc);
|
||||
}
|
||||
|
||||
|
|
@ -71,9 +71,9 @@ Ox.getDaysInMonth <f> Get the number of days in a given month
|
|||
Ox.getDaysInMonth = function(year, month) {
|
||||
year = Ox.makeYear(year);
|
||||
month = Ox.isNumber(month) ? month
|
||||
: Ox.map(Ox.MONTHS, function(v, i) {
|
||||
return v.substr(0, 3) == month.substr(0, 3) ? i + 1 : null;
|
||||
})[0];
|
||||
: Ox.getIndexOf(Ox.MONTHS, function(v) {
|
||||
return v.substr(0, 3) == month.substr(0, 3);
|
||||
}) + 1;
|
||||
return new Date(year, month, 0).getDate();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -279,8 +279,9 @@
|
|||
Ox.encodeHTML = function(str) {
|
||||
return Ox.map(str.toString(), function(v) {
|
||||
var code = v.charCodeAt(0);
|
||||
return code < 128 ? (v in Ox.HTML_ENTITIES ? Ox.HTML_ENTITIES[v] : v) :
|
||||
'&#x' + Ox.pad(code.toString(16).toUpperCase(), 4) + ';';
|
||||
return code < 128
|
||||
? (v in Ox.HTML_ENTITIES ? Ox.HTML_ENTITIES[v] : v)
|
||||
: '&#x' + Ox.pad(code.toString(16).toUpperCase(), 4) + ';';
|
||||
}).join('');
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -366,9 +366,9 @@ Ox.formatDateRangeDuration = function(start, end, utc) {
|
|||
}
|
||||
}
|
||||
});
|
||||
return Ox.map(values, function(value, i) {
|
||||
return value ? value + ' ' + keys[i] + (value > 1 ? 's' : '') : null;
|
||||
}).join(' ');
|
||||
return Ox.filter(Ox.map(values, function(value, i) {
|
||||
return value ? value + ' ' + keys[i] + (value > 1 ? 's' : '') : '';
|
||||
})).join(' ');
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
@ -450,7 +450,7 @@ Ox.formatDuration = function(/*sec, dec, format*/) {
|
|||
str.shift();
|
||||
pad.shift();
|
||||
}
|
||||
return Ox.map(val, function(v, i) {
|
||||
return Ox.filter(Ox.map(val, function(v, i) {
|
||||
var ret;
|
||||
if (!format) {
|
||||
ret = Ox.pad(v, pad[i]);
|
||||
|
|
@ -458,10 +458,10 @@ Ox.formatDuration = function(/*sec, dec, format*/) {
|
|||
ret = v + (format == 'long' ? ' ' : '') + str[i]
|
||||
+ (format == 'long' && v != 1 ? 's' : '');
|
||||
} else {
|
||||
ret = null;
|
||||
ret = '';
|
||||
}
|
||||
return ret;
|
||||
}).join(!format ? ':' : ' ');
|
||||
})).join(!format ? ':' : ' ');
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
|
|||
|
|
@ -356,11 +356,11 @@
|
|||
ne: {lat: 90, lng: ret.sw.lng}
|
||||
}];
|
||||
function containsGaps(area) {
|
||||
return Ox.map(gaps, function(gap, i) {
|
||||
return Ox.getIndices(gaps, function(gap) {
|
||||
return Ox.containsArea({
|
||||
sw: {lat: -90, lng: area.sw.lng},
|
||||
ne: {lat: 90, lng: area.ne.lng}
|
||||
}, gap) ? i : null;
|
||||
}, gap);
|
||||
});
|
||||
}
|
||||
function intersectsWithGaps(area) {
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ Ox.doc <f> Generates documentation for annotated JavaScript
|
|||
Ox.doc = (function() {
|
||||
// fixme: dont require the trailing '@'
|
||||
var re = {
|
||||
item: /^(.+?) <(.+?)> (.+)$/,
|
||||
item: /^(.+?) <(.+?)>(.*?)$/,
|
||||
multiline: /^\/\*\@.*?\n([\w\W]+)\n.*?\@\*\/$/,
|
||||
script: /\n(\s*<script>s*\n[\w\W]+\n\s*<\/script>s*)/g,
|
||||
singleline: /^\/\/@\s*(.*?)\s*$/,
|
||||
|
|
@ -122,9 +122,10 @@ Ox.doc = (function() {
|
|||
var lines = decodeLinebreaks(str).split('\n'),
|
||||
indent = getIndent(lines[1]);
|
||||
return {
|
||||
statement: Ox.map(lines, function(line, i) {
|
||||
return i && i < lines.length - 1 ?
|
||||
line.substr(indent) : null;
|
||||
statement: Ox.filter(lines, function(line, i) {
|
||||
return i && i < lines.length - 1;
|
||||
}).map(lines, function(line, i) {
|
||||
return line.substr(indent);
|
||||
}).join('\n')
|
||||
};
|
||||
}
|
||||
|
|
@ -336,7 +337,7 @@ Ox.minify = function() {
|
|||
'number', 'method', 'object', 'property'
|
||||
].indexOf(token.type) > -1;
|
||||
}
|
||||
return Ox.map(tokens, function(token, i) {
|
||||
return Ox.filter(Ox.map(tokens, function(token, i) {
|
||||
var ret = source.substr(token.offset, token.length);
|
||||
if (token.type == 'comment') {
|
||||
if (ret[1] == '/') {
|
||||
|
|
@ -348,8 +349,7 @@ Ox.minify = function() {
|
|||
ret = i == length - 1 || tokens[i + 1].type == 'whitespace'
|
||||
? null : ' ';
|
||||
}
|
||||
}
|
||||
if (token.type == 'linebreak' || token.type == 'whitespace') {
|
||||
} else if (token.type == 'linebreak' || token.type == 'whitespace') {
|
||||
// remove consecutive linebreaks or whitespace
|
||||
ret = ret[0];
|
||||
}
|
||||
|
|
@ -392,7 +392,7 @@ Ox.minify = function() {
|
|||
ret = null;
|
||||
}
|
||||
return ret;
|
||||
}).join('');
|
||||
})).join('');
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -31,9 +31,9 @@ Ox.clean <f> Remove leading, trailing and double whitespace from a string
|
|||
"foo bar"
|
||||
@*/
|
||||
Ox.clean = function(str) {
|
||||
return Ox.map(str.split('\n'), function(str) {
|
||||
return Ox.trim(str.replace(/\s+/g, ' ')) || null;
|
||||
}).join('\n');
|
||||
return Ox.filter(Ox.map(str.split('\n'), function(str) {
|
||||
return Ox.trim(str.replace(/\s+/g, ' ')) || '';
|
||||
})).join('\n');
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
@ -481,7 +481,7 @@ Ox.toTitleCase <f> Returns a string with capitalized words
|
|||
'Apple Releases iPhone, IBM Stock Plummets'
|
||||
@*/
|
||||
Ox.toTitleCase = function(str) {
|
||||
return Ox.map(str.split(' '), function(val) {
|
||||
return str.split(' ').map(function(val) {
|
||||
var sub = val.substr(1),
|
||||
low = sub.toLowerCase();
|
||||
if (sub == low) {
|
||||
|
|
@ -603,7 +603,7 @@ Ox.wordwrap = function(str, len, sep, bal, spa) {
|
|||
if (lines.length > 1) {
|
||||
// test shorter line, unless
|
||||
// that means cutting a word
|
||||
var max = Ox.max(Ox.map(words, function(word) {
|
||||
var max = Ox.max(words.map(function(word) {
|
||||
return word.length;
|
||||
}));
|
||||
while (len > max) {
|
||||
|
|
@ -637,7 +637,7 @@ Ox.wordwrap = function(str, len, sep, bal, spa) {
|
|||
}
|
||||
});
|
||||
if (!spa) {
|
||||
lines = Ox.map(lines, function(line) {
|
||||
lines = lines.map(function(line) {
|
||||
return Ox.trim(line);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue