From 82da04407b793822bca504d3570ac5a30b45c30e Mon Sep 17 00:00:00 2001 From: Sergey Homa Date: Sun, 14 Jan 2018 22:12:53 +0200 Subject: [PATCH] feat: add propNotEq (#275) Closes #238 --- src/index.d.ts | 11 +++++++++++ src/index.js | 1 + src/propNotEq.js | 23 +++++++++++++++++++++++ test/propNotEq.js | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 src/propNotEq.js create mode 100644 test/propNotEq.js diff --git a/src/index.d.ts b/src/index.d.ts index 3e3d9d8de9..a1776da2a9 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -569,6 +569,17 @@ declare namespace RamdaAdjunct { from(lens: Function): Function } + /** + * Returns true if the specified object property is not equal, + * in R.equals terms, to the given value; false otherwise. + */ + propNotEq(prop: string|number, value: any, obj: object): boolean; + propNotEq(prop: string|number, value: any): (obj: object) => boolean; + propNotEq(prop: string|number): { + (value: any, obj: object): boolean; + (value: any): (obj: object) => boolean; + } + /** * Returns whether or not an object has an own property with the specified name at a given path. */ diff --git a/src/index.js b/src/index.js index d44cb75d96..657a5253db 100644 --- a/src/index.js +++ b/src/index.js @@ -115,6 +115,7 @@ export { default as lensNotEq } from './lensNotEq'; export { default as lensSatisfies } from './lensSatisfies'; export { default as lensNotSatisfy } from './lensNotSatisfy'; export { default as lensIso } from './lensIso'; +export { default as propNotEq } from './propNotEq'; // Logic export { default as defaultWhen } from './defaultWhen'; export { default as notBoth } from './notBoth'; diff --git a/src/propNotEq.js b/src/propNotEq.js new file mode 100644 index 0000000000..14959eef05 --- /dev/null +++ b/src/propNotEq.js @@ -0,0 +1,23 @@ +import { propEq, complement, curryN } from 'ramda'; + +/** + * Returns true if the specified object property is not equal, + * in R.equals terms, to the given value; false otherwise. + * + * @func propNotEq + * @memberOf RA + * @since {@link https://char0n.github.io/ramda-adjunct/1.1.0|v1.1.0} + * @category Relation + * @sig String → a → Object → Boolean + * @param {String} prop The prop to pick + * @param {a} value The value to compare to + * @param {Object} object The object, that presumably contains value under the prop + * @return {Boolean} Comparison result + * @see {@link http://ramdajs.com/docs/#propEq|propEq} + * @example + * + * RA.propNotEq('Not you!', 'a', { a: 'foo', b: 'bar' }); //=> true + */ +const propNotEq = curryN(3, complement(propEq)); + +export default propNotEq; diff --git a/test/propNotEq.js b/test/propNotEq.js new file mode 100644 index 0000000000..347fdcd0dd --- /dev/null +++ b/test/propNotEq.js @@ -0,0 +1,41 @@ +import * as RA from '../src/index'; +import eq from './shared/eq'; + + +describe('propNotEq', function() { + let prop; + let value; + let obj; + let expected; + + beforeEach(function() { + prop = 'a'; + value = 'foo'; + obj = { a: 1, b: 2 }; + expected = true; + }); + + it('tests currying', function() { + eq(RA.propNotEq(prop, value, obj), expected); + eq(RA.propNotEq(prop)(value, obj), expected); + eq(RA.propNotEq(prop, value)(obj), expected); + eq(RA.propNotEq(prop)(value)(obj), expected); + }); + + it('tests prop value is not equal', function() { + eq(RA.propNotEq(prop, value, obj), expected); + }); + + it('tests prop value is equal', function() { + value = 1; + expected = false; + + eq(RA.propNotEq(prop, value, obj), expected); + }); + + it('tests no prop', function() { + prop = 'bar'; + + eq(RA.propNotEq(prop, value, obj), expected); + }); +});