diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..85de9cf --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +src diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..fc1da66 --- /dev/null +++ b/dist/index.js @@ -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; \ No newline at end of file diff --git a/package.json b/package.json index bad7e24..395e885 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/lib/index.js b/src/index.js similarity index 100% rename from lib/index.js rename to src/index.js diff --git a/test/index.js b/test/index.js index 2c8d429..17f5101 100644 --- a/test/index.js +++ b/test/index.js @@ -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 () {