utils: CoffeeScript → ES6.

This commit is contained in:
Julien Fontanet
2015-02-12 15:58:29 +01:00
parent a1d819dcb6
commit c835cf7829
5 changed files with 62 additions and 72 deletions

View File

@@ -10,7 +10,7 @@ $isObject = require 'lodash.isobject'
$isString = require 'lodash.isstring'
$map = require 'lodash.map'
{$mapInPlace, $wrap} = require './utils'
{mapInPlace: $mapInPlace, wrap: $wrap} = require './utils'
#=====================================================================

View File

@@ -21,6 +21,7 @@ var InvalidParameters = require('./api-errors').InvalidParameters;
var MethodNotFound = require('./api-errors').MethodNotFound;
var Unauthorized = require('./api-errors').Unauthorized;
var wait = require('./fibers-utils').$wait;
var wrap = require('./utils').wrap;
//====================================================================
@@ -33,12 +34,6 @@ function $deprecated(fn)
};
}
var wrap = function (val) {
return function () {
return val;
};
};
//====================================================================
// TODO: Helper functions that could be written:

View File

@@ -1,64 +0,0 @@
{promisify: $promisify} = require 'bluebird'
$randomBytes = $promisify (require 'crypto').randomBytes
$base64url = require 'base64url'
#=====================================================================
$done = exports.$done = {}
exports.$generateToken = (n = 32) -> ($randomBytes n).then $base64url
# Similar to `lodash.map()` for array and `lodash.mapValues()` for objects.
#
# Note: can be interrupted by returning the special value `done`
# provided as the forth argument.
exports.$map = (col, iterator, ctx) ->
# The default context is inherited.
ctx ?= this
if (n = col.length)?
result = []
# Array-like object.
i = 0
while i < n
value = iterator.call ctx, col[i], "#{i}", col, $done
break if value is $done
result.push value
++i
else
result = {}
for key of col
value = iterator.call ctx, col[key], key, $done
break if value is $done
result.push value
return result
# Similar to `$map()` but change the current collection.
#
# Note: can be interrupted by returning the special value `done`
# provided as the forth argument.
exports.$mapInPlace = (col, iterator, ctx) ->
# The default context is inherited.
ctx ?= this
if (n = col.length)?
# Array-like object.
i = 0
while i < n
value = iterator.call ctx, col[i], "#{i}", col, $done
break if value is $done
col[i] = value
++i
else
for key of col
value = iterator.call ctx, col[key], key, $done
break if value is $done
col[key] = value
return col
# Wraps a value in a function.
exports.$wrap = (val) -> -> val

59
src/utils.js Normal file
View File

@@ -0,0 +1,59 @@
import base64url from 'base64url';
import forEach from 'lodash.foreach';
import has from 'lodash.has';
import {promisify} from 'bluebird';
import {randomBytes} from 'crypto';
randomBytes = promisify(randomBytes);
//====================================================================
// Generate a secure random Base64 string.
let generateToken = (n = 32) => randomBytes(n).then(base64url);
exports.generateToken = generateToken;
// Special value which can be returned to stop an iteration in map()
// and mapInPlace().
let done = {};
exports.done = done;
// Similar to `lodash.map()` for array and `lodash.mapValues()` for
// objects.
//
// Note: can be interrupted by returning the special value `done`
// provided as the forth argument.
function map(col, iterator, thisArg = this) {
let result = has(col, 'length') ? [] : {};
forEach(col, (item, i) => {
let value = iterator.call(thisArg, item, i, done);
if (value === done) {
return false;
}
result[i] = value;
});
return result;
}
exports.map = map;
// Similar to `map()` but change the current collection.
//
// Note: can be interrupted by returning the special value `done`
// provided as the forth argument.
function mapInPlace(col, iterator, thisArg = this) {
forEach(col, (item, i) => {
let value = iterator.call(thisArg, item, i, done);
if (value === done) {
return false;
}
col[i] = value;
});
return col;
}
exports.mapInPlace = mapInPlace;
// Wrap a value in a function.
let wrap = (value) => () => value;
exports.wrap = wrap;

View File

@@ -23,7 +23,7 @@ $RedisCollection = require './collection/redis'
$spec = require './spec'
$XAPI = require './xapi'
{$coroutine, $fiberize, $wait} = require './fibers-utils'
{$generateToken} = require './utils'
{generateToken: $generateToken} = require './utils'
{$MappedCollection} = require './MappedCollection'
#=====================================================================