Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
146 changes: 146 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
'use strict';

var R = require('ramda');
var is = require('is_js');

var Ru = R.map(R.curry, {
applyTo: function applyTo(obj, fn) {
return fn(obj);
},
compareProps: function compareProps(props, a, b) {
// determine property compare function (lt or gt) based on + or -
var propCompares = R.map(function (prop) {
return prop[0] == '-' ? R.gt : R.lt;
}, props);
// remove + and - from property names
props = R.map(R.replace(/^(-|\+)/, ''), props);
// determine which properties are equal
var equalProps = R.map(function (prop) {
return R.equals(a[prop], b[prop]);
}, props);
// find first non-equal property
var index = R.findIndex(R.equals(false), equalProps);
// if found then compare that property
if (index >= 0) return R.comparator(propCompares[index])(a[props[index]], b[props[index]]);
// return all properties equal
return 0;
},
complementC: function complementC(fn) {
return R.curry(R.nAry(fn.length, function () {
return !fn.apply(this, arguments);
}));
},
defaults: function defaults(def, obj) {
return R.merge(def, Ru.filterObj(R.complement(is.undefined), obj));
},
defaultsR: function defaultsR(def, obj) {
return Ru.mergeR(def, Ru.filterObjR(R.complement(is.undefined), obj));
},
mergeR: function mergeR(a, b) {
var obj = R.clone(a);
R.forEach(function (key) {
return obj[key] = is.json(obj[key]) && is.json(b[key]) ? Ru.mergeR(obj[key] || {}, b[key]) : b[key];
}, R.keys(b));
return obj;
},
filterObj: function filterObj(pred, obj) {
return R.pick(R.filter(function (key) {
return pred(obj[key]);
}, R.keys(obj)), obj);
},
filterObjR: function filterObjR(pred, obj) {
return R.map(function (v) {
return is.json(v) ? Ru.filterObjR(pred, v) : v;
}, Ru.filterObj(R.anyPass([is.json, pred]), obj));
},
isEmptyC: function isEmptyC(fn) {
return R.curry(R.nAry(fn.length, function () {
return R.isEmpty(fn.apply(this, arguments));
}));
},
isNotEmptyC: function isNotEmptyC(fn) {
return R.curry(R.nAry(fn.length, function () {
return !R.isEmpty(fn.apply(this, arguments));
}));
},
matchGroups: function matchGroups(reg, str) {
var m,
g = [];
// avoid infinite loop
if (!reg.global) {
var match = reg.exec(str);
if (match) g.push(match.slice(1));
} else {
var previousIndex = -1;
while ((m = reg.exec(str)) && previousIndex != m.index) {
g.push(m.slice(1));
previousIndex = m.index;
}
}
return g;
},
pickValues: function pickValues(keys, obj) {
return R.compose(R.values, R.pick(keys))(obj);
},
rmap: function rmap(obj, fns) {
return R.map(Ru.applyTo(obj), fns);
},
sumProps: function sumProps(keys, obj) {
return R.sum(Ru.pickValues(keys, obj));
},
sumColumn: function sumColumn(key, objs) {
return R.sum(R.map(R.prop(key), objs));
},
subsetOf: function subsetOf(set, sub) {
return R.reduce(R.and, true, R.map(R.contains(R.__, set), sub));
},
toNumber: function toNumber(x) {
return Number(x);
},
toString: function toString(x) {
return String(x);
},
toDate: function toDate(x) {
return new Date(x);
},
trace: function trace(msg, x) {
console.log(msg, x);return x;
},
zipApply: function zipApply(fns, objs) {
return R.zipWith(R.call, fns, objs);
},
substring: function substring(start, end, str) {
return str.substring(start, end || undefined);
},
pathCommon: function pathCommon(delimiter, path, obj) {
return R.path(R.split(delimiter, path))(obj);
}
});

Ru = R.merge(Ru, R.map(R.curry, {
createIndexOpts: function createIndexOpts(options, keys, objs) {
options = R.merge({
unique: false,
keyDelimiter: '|'
}, options || {});
return R.reduce(function (objIndex, obj) {
var indexKey = Ru.pickValues(keys, obj).join(options.keyDelimiter);
// create empty entry
if (!objIndex[indexKey]) objIndex[indexKey] = [];else if (options.unique) throw new Error('Cannot build unique index (index key: ' + indexKey + ')');
// add to existing entry
objIndex[indexKey].push(obj);
return objIndex;
}, {}, objs);
}
}));

Ru = R.merge(Ru, {
mergeAllR: function mergeAllR(a) {
return R.reduce(Ru.mergeR, {}, a);
},
path: Ru.pathCommon('.')
});

Ru.createIndex = Ru.createIndexOpts(null);

module.exports = Ru;
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"version": "0.2.8",
"description": "Utilities built on top of Ramda.",
"repository": "panosoft/ramda-utils",
"main": "lib/index.js",
"main": "dist/index.js",
"scripts": {
"test": "mocha",
"patch": "npm version patch && git push --follow-tags && npm publish",
"minor": "npm version minor && git push --follow-tags && npm publish"
"minor": "npm version minor && git push --follow-tags && npm publish",
"build": "babel src --presets babel-preset-es2015 --out-dir dist",
"prepublish": "npm run build"
},
"author": "Charles Scalfani",
"license": "MIT",
Expand All @@ -16,6 +18,8 @@
"ramda": "^0.18.0"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-preset-es2015": "^6.24.1",
"chai": "^3.2.0",
"mocha": "^2.3.2",
"sinon": "^1.16.1",
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var expect = require('chai')
.expect;
var assert = require('chai').assert;
var R = require('ramda');
var Ru = require('../lib');
var Ru = require('../src');
var sinon = require('sinon');

describe('applyTo', function () {
Expand Down