From dcad5d7c72244e238d069a0726e906afad035db7 Mon Sep 17 00:00:00 2001 From: ch-shubham Date: Fri, 5 Jun 2020 07:57:33 +0530 Subject: [PATCH 1/7] adding propEq and removing comments from not.spec file --- mod.ts | 1 + propEq.ts | 19 +++++++++++++++++++ specs/PropEq.spec.ts | 14 ++++++++++++++ specs/not.spec.ts | 5 ----- 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 propEq.ts create mode 100644 specs/PropEq.spec.ts diff --git a/mod.ts b/mod.ts index 467ebdb..fbb08a5 100644 --- a/mod.ts +++ b/mod.ts @@ -85,6 +85,7 @@ export { paths, Path } from './paths.ts' export { pipe } from './pipe.ts' export { prepend } from './prepend.ts' export { prop } from './prop.ts' +export { propEq } from './propEq.ts' export { props } from './props.ts' export { range } from './range.ts' export { rangeUntil } from './rangeUntil.ts' diff --git a/propEq.ts b/propEq.ts new file mode 100644 index 0000000..0798e79 --- /dev/null +++ b/propEq.ts @@ -0,0 +1,19 @@ +import curryN from "./utils/curry_n.ts" +import { Curry3, ObjRec } from "./utils/types.ts" +import { equals } from './equals.ts' + +function _propEq(name: string, val: any, obj: ObjRec) { + return equals(val, obj[name]) +} + +/** + * Returns `true` if the specified object property is equal, to the given value; `false` otherwise. + * + * const shivam = {name: 'shivam', age: 20, hair: 'brown'} + * const shubham = {name: 'shubham', age: 22, hair: 'black'} + * const Krish = {name: 'krish', age: 25, hair: 'black'} + * const students = [abby, fred, rusty, alois] + * const hasBrownHair = Fae.propEq('hair', 'brown') + * Fae.filter(hasBrownHair, kids) //=> [shubham] + */ +export const propEq: Curry3 = curryN(2, _propEq) diff --git a/specs/PropEq.spec.ts b/specs/PropEq.spec.ts new file mode 100644 index 0000000..2237b54 --- /dev/null +++ b/specs/PropEq.spec.ts @@ -0,0 +1,14 @@ +import { describe, it, expect } from "./_describe.ts" +import { propEq } from '../mod.ts' +import { eq } from "./utils/utils.ts" + +describe('propEq', () => { + let obj1 = {name: 'shubham', age: 22, hair: 'blue'} + let obj2 = {name: 'Shivam', age: 21, hair: 'black'} + + it('should determine property matching a given value for a specific object properly', () => { + eq(propEq('name', 'shubham', obj1), true) + eq(propEq('hair', 'black', obj2), true) + eq(propEq('hair', 'blue', obj2), false) + }) +}) \ No newline at end of file diff --git a/specs/not.spec.ts b/specs/not.spec.ts index 3d58bf3..a951206 100644 --- a/specs/not.spec.ts +++ b/specs/not.spec.ts @@ -2,11 +2,6 @@ import { describe, it, expect } from "./_describe.ts" import { not } from '../mod.ts' import { eq } from "./utils/utils.ts" -/** - * @function - * takes an argument and returns its not -*/ - describe('not', () => { it('should be properly declared',() => { eq(not(true), false) From 15f3ed258b8ca8573a203b0c09fe9cf889ff3827 Mon Sep 17 00:00:00 2001 From: ch-shubham Date: Fri, 5 Jun 2020 08:30:07 +0530 Subject: [PATCH 2/7] adding propIs --- mod.ts | 1 + propEq.ts | 2 +- propIs.ts | 17 +++++++++++++++++ specs/propIs.spec.ts | 14 ++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 propIs.ts create mode 100644 specs/propIs.spec.ts diff --git a/mod.ts b/mod.ts index fbb08a5..cbdcab5 100644 --- a/mod.ts +++ b/mod.ts @@ -86,6 +86,7 @@ export { pipe } from './pipe.ts' export { prepend } from './prepend.ts' export { prop } from './prop.ts' export { propEq } from './propEq.ts' +export { propIs } from './propIs.ts' export { props } from './props.ts' export { range } from './range.ts' export { rangeUntil } from './rangeUntil.ts' diff --git a/propEq.ts b/propEq.ts index 0798e79..10e6a05 100644 --- a/propEq.ts +++ b/propEq.ts @@ -16,4 +16,4 @@ function _propEq(name: string, val: any, obj: ObjRec) { * const hasBrownHair = Fae.propEq('hair', 'brown') * Fae.filter(hasBrownHair, kids) //=> [shubham] */ -export const propEq: Curry3 = curryN(2, _propEq) +export const propEq: Curry3 = curryN(3, _propEq) diff --git a/propIs.ts b/propIs.ts new file mode 100644 index 0000000..cc23dff --- /dev/null +++ b/propIs.ts @@ -0,0 +1,17 @@ +import curryN from "./utils/curry_n.ts" +import { Curry3, Func, Obj } from "./utils/types.ts" +import { is } from "./utils/is.ts"; + +function _propIs(type: string, name: string, obj: Obj) { + return is(obj[name], type) +} + +/** + * Returns `true` if the specified object property is of the given type; + * `false` otherwise. + * + * Fae.propIs(Number, 'a', {a: 1, y: 2}) //=> true + * Fae.propIs(String, 'a', {a: 'foo'}) //=> true + * Fae.propIs(Number, 'a', {}) //=> false + */ +export const propIs: Curry3 = curryN(3, _propIs) diff --git a/specs/propIs.spec.ts b/specs/propIs.spec.ts new file mode 100644 index 0000000..b3ef370 --- /dev/null +++ b/specs/propIs.spec.ts @@ -0,0 +1,14 @@ +import { describe, it } from "./_describe.ts" +import { propIs } from '../mod.ts' +import { eq } from "./utils/utils.ts" + +describe('propEq', () => { + it('should return true if the specified object property is of the given type', () => { + eq(propIs(Number, 'a', {a: 1, y: 2}), true) + }) + + it('Should return false otherwise', () => { + eq(propIs(String, 'ob', {ob: 1}), false) + eq(propIs(String, 'ob', {}), false) + }) +}) \ No newline at end of file From e6dde553069828c22168c68692e52327477a6462 Mon Sep 17 00:00:00 2001 From: ch-shubham Date: Fri, 5 Jun 2020 08:50:56 +0530 Subject: [PATCH 3/7] adding propSatisfies and eqProps --- eqProps.ts | 19 +++++++++++++++++++ mod.ts | 2 ++ propSatisfies.ts | 15 +++++++++++++++ specs/eqProps.spec.ts | 13 +++++++++++++ specs/propSatisfies.spec.ts | 15 +++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 eqProps.ts create mode 100644 propSatisfies.ts create mode 100644 specs/eqProps.spec.ts create mode 100644 specs/propSatisfies.spec.ts diff --git a/eqProps.ts b/eqProps.ts new file mode 100644 index 0000000..8491fbc --- /dev/null +++ b/eqProps.ts @@ -0,0 +1,19 @@ +import { Curry3, Obj } from "./utils/types.ts" +import curryN from "./utils/curry_n.ts" +import { equals } from "./equals.ts" + +function _eqProps(prop: string, obj1: Obj, obj2: Obj) { + return equals(obj1[prop], obj2[prop]) +} + + +/** + * Reports whether two objects have the same value, for the specified property. + * Useful as a curried predicate. + * + * const obj1 = { a: 1, b: 2, c: 3, d: 4 } + * const obj2 = { a: 10, b: 20, c: 3, d: 40 } + * Fae.eqProps('a', obj1, obj2) //=> false + * Fae.eqProps('c', obj1, obj2) //=> true + */ +export const eqProps: Curry3 = curryN(3, _eqProps) diff --git a/mod.ts b/mod.ts index cbdcab5..ca2f04e 100644 --- a/mod.ts +++ b/mod.ts @@ -45,6 +45,7 @@ export { dropRepeats } from './dropRepeats.ts' export { dropRepeatsWith } from './dropRepeatsWith.ts' export { dropWhile } from './dropWhile.ts' export { endsWith } from './endsWith.ts' +export { eqProps } from './eqProps.ts' export { equals } from './equals.ts' export { filter } from './filter.ts' export { find } from './find.ts' @@ -88,6 +89,7 @@ export { prop } from './prop.ts' export { propEq } from './propEq.ts' export { propIs } from './propIs.ts' export { props } from './props.ts' +export { propSatisfies } from './propSatisfies.ts' export { range } from './range.ts' export { rangeUntil } from './rangeUntil.ts' export { reduce } from './reduce.ts' diff --git a/propSatisfies.ts b/propSatisfies.ts new file mode 100644 index 0000000..eeff529 --- /dev/null +++ b/propSatisfies.ts @@ -0,0 +1,15 @@ +import { ObjRec, Predicate1, Curry3 } from "./utils/types.ts" +import curryN from "./utils/curry_n.ts" + +function _propSatisfies(pred: Predicate1, name: string, obj: ObjRec) { + return pred(obj[name]); +} + + +/** + * Returns `true` if the specified object property satisfies the given + * predicate; `false` otherwise. + * + * Fae.propSatisfies(x => x > 0, 'x', {x: 1, y: 2}); //=> true + */ +export const propSatisfies: Curry3 = curryN(3, _propSatisfies) diff --git a/specs/eqProps.spec.ts b/specs/eqProps.spec.ts new file mode 100644 index 0000000..f4ff0ea --- /dev/null +++ b/specs/eqProps.spec.ts @@ -0,0 +1,13 @@ +import { describe, it, expect } from "./_describe.ts" +import { eqProps } from '../mod.ts' +import { eq } from "./utils/utils.ts" + +describe('propEq', () => { + it('should return true when two objects have the same value for a given property', function() { + eq(eqProps('name', {name: 'shubham', age: 10}, {name: 'shubham', age: 12}), true) + eq(eqProps('name', {name: 'shivam', age: 10}, {name: 'shubham', age: 10}), false) + eq(eqProps('value', {value: 0}, {value: -0}), false) + eq(eqProps('value', {value: -0}, {value: 0}), false) + eq(eqProps('value', {value: NaN}, {value: NaN}), true) + }) +}) \ No newline at end of file diff --git a/specs/propSatisfies.spec.ts b/specs/propSatisfies.spec.ts new file mode 100644 index 0000000..cab5d5b --- /dev/null +++ b/specs/propSatisfies.spec.ts @@ -0,0 +1,15 @@ +import { describe, it, expect } from "./_describe.ts" +import { propSatisfies } from '../mod.ts' +import { eq } from "./utils/utils.ts" + +describe('propEq', () => { + let isPositive = (n: number) => n > 0 + + it('should return true if the specified object property satisfies the given predicate', () => { + eq(propSatisfies(isPositive, 'x', {x: 1, y: 0}), true) + }) + + it('should return false otherwise', () => { + eq(propSatisfies(isPositive, 'y', {x: 1, y: 0}), false) + }) +}) \ No newline at end of file From a82c9c0411953f6d1b64a5799f6b6514375814a3 Mon Sep 17 00:00:00 2001 From: ch-shubham Date: Fri, 5 Jun 2020 10:26:12 +0530 Subject: [PATCH 4/7] adding defaultTo, pathOr, propOr --- defaultTo.ts | 21 ++++++++++++++++ mod.ts | 3 +++ pathOr.ts | 18 +++++++++++++ propOr.ts | 23 +++++++++++++++++ specs/defaultTo.spec.ts | 30 ++++++++++++++++++++++ specs/pathOr.spec.ts | 50 +++++++++++++++++++++++++++++++++++++ specs/propOr.spec.ts | 31 +++++++++++++++++++++++ specs/propSatisfies.spec.ts | 2 +- 8 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 defaultTo.ts create mode 100644 pathOr.ts create mode 100644 propOr.ts create mode 100644 specs/defaultTo.spec.ts create mode 100644 specs/pathOr.spec.ts create mode 100644 specs/propOr.spec.ts diff --git a/defaultTo.ts b/defaultTo.ts new file mode 100644 index 0000000..ac98ee2 --- /dev/null +++ b/defaultTo.ts @@ -0,0 +1,21 @@ +import { Curry2 } from "./utils/types.ts" +import curryN from "./utils/curry_n.ts" + +function _defaultTo(defaultV: any, value: any) { + return value == null || value !== value ? defaultV : value; +} + + +/** + * Returns the second argument if it is not `null`, `undefined` or `NaN`; + * otherwise the first argument is returned. + * + * + * const defaultTo125 = Fae.defaultTo(125) + * + * defaultTo125(null) //=> 125 + * defaultTo125(undefined) //=> 125 + * defaultTo125(false) //=> false + * defaultTo125('Fae') //=> 'Fae' + */ +export const defaultTo: Curry2 = curryN(2, _defaultTo) diff --git a/mod.ts b/mod.ts index ca2f04e..1706324 100644 --- a/mod.ts +++ b/mod.ts @@ -35,6 +35,7 @@ export { contains } from './contains.ts' export { crossProduct } from './crossProduct.ts' export { curry } from './curry.ts' export { dec } from './dec.ts' +export { defaultTo } from './defaultTo.ts' export { dissoc } from './dissoc.ts' export { dissocPath } from './dissocPath.ts' export { divide } from './divide.ts' @@ -82,12 +83,14 @@ export { not } from './not.ts' export { nth } from './nth.ts' export { over } from './over.ts' export { path } from './path.ts' +export { pathOr } from './pathOr.ts' export { paths, Path } from './paths.ts' export { pipe } from './pipe.ts' export { prepend } from './prepend.ts' export { prop } from './prop.ts' export { propEq } from './propEq.ts' export { propIs } from './propIs.ts' +export { propOr } from './propOr.ts' export { props } from './props.ts' export { propSatisfies } from './propSatisfies.ts' export { range } from './range.ts' diff --git a/pathOr.ts b/pathOr.ts new file mode 100644 index 0000000..b62dafb --- /dev/null +++ b/pathOr.ts @@ -0,0 +1,18 @@ +import { Curry3, Obj } from "./utils/types.ts" +import curryN from "./utils/curry_n.ts" +import { defaultTo } from './defaultTo.ts' +import { path } from './path.ts' + +function _pathOr(d: any, p: Array, obj: any) { + return defaultTo(d, path(p, obj)) +} + +/** + * If the given, non-null object has a value at the given path, returns the + * value at that path. Otherwise returns the provided default value. + * + * + * Fae.pathOr('N/A', ['a', 'b'], {a: {b: 2}}); //=> 2 + * Fae.pathOr('N/A', ['a', 'b'], {c: {b: 2}}); //=> "N/A" + */ +export const pathOr: Curry3, any, any> = curryN(3, _pathOr) \ No newline at end of file diff --git a/propOr.ts b/propOr.ts new file mode 100644 index 0000000..2f4ef8b --- /dev/null +++ b/propOr.ts @@ -0,0 +1,23 @@ +import curryN from "./utils/curry_n.ts" +import { Curry3, Obj } from "./utils/types.ts" +import { pathOr } from "./pathOr.ts" + +function _propOr(val: any, p: string, obj: Obj | null) { + return pathOr(val, [p], obj) +} + +/** + * If the given, non-null object has an own property with the specified name, + * returns the value of that property. Otherwise returns the provided default + * value. + * const alice = { + * name: 'Fae', + * age: 15 + * }; + * const Great = Fae.prop('GreatLibrary'); + * const GreatWithDefault = Fae.propOr('FaeModule', 'GreatLibrary'); + * + * Great(Fae); //=> undefined + * GreatWithDefault(Fae); //=> 'FaeModule' + */ +export const propOr: Curry3 = curryN(3, _propOr) diff --git a/specs/defaultTo.spec.ts b/specs/defaultTo.spec.ts new file mode 100644 index 0000000..fc84af9 --- /dev/null +++ b/specs/defaultTo.spec.ts @@ -0,0 +1,30 @@ +import { describe, it } from "./_describe.ts" +import { defaultTo } from '../mod.ts' +import { eq } from "./utils/utils.ts" + +describe('defaultTo', () => { + + let defaultTo125 = defaultTo(125) + + it('should return the default value if input is null, undefined or NaN', () => { + eq(125, defaultTo125(null)) + eq(125, defaultTo125(undefined)) + eq(125, defaultTo125(NaN)) + }) + + it('should return the input value if it is not null/undefined', () => { + eq('a real value', defaultTo125('a real value')) + }) + + it('should return the input value even if it is considered falsy', () => { + eq('', defaultTo125('')) + eq(0, defaultTo125(0)) + eq(false, defaultTo125(false)) + eq([], defaultTo125([])) + }) + + it('should be called with both arguments directly', () => { + eq(125, defaultTo(125, null)) + eq('a real value', defaultTo(125, 'a real value')) + }) +}) \ No newline at end of file diff --git a/specs/pathOr.spec.ts b/specs/pathOr.spec.ts new file mode 100644 index 0000000..d592287 --- /dev/null +++ b/specs/pathOr.spec.ts @@ -0,0 +1,50 @@ +import { describe, it } from "./_describe.ts" +import { pathOr } from '../mod.ts' +import { eq } from "./utils/utils.ts" + +describe('pathOr', () => { + let deepObject = {a: {b: {c: 'c'}}, falseVal: false, nullVal: null, undefinedVal: undefined, arrayVal: ['arr']} + + it('takes a path and an object and returns the value at the path or the default value', () => { + let obj = { + a: { + b: { + c: 100, + d: 200 + }, + e: { + f: [100, 101, 102], + g: 'G' + }, + h: 'H' + }, + i: 'I', + j: ['J'] + } + eq(pathOr('Unknown', ['a', 'b', 'c'], obj), 100) + eq(pathOr('Unknown', [], obj), obj) + eq(pathOr('Unknown', ['a', 'e', 'f', 1], obj), 101) + eq(pathOr('Unknown', ['j', 0], obj), 'J') + eq(pathOr('Unknown', ['j', 1], obj), 'Unknown') + eq(pathOr('Unknown', ['a', 'b', 'c'], null), 'Unknown') + }) + + it("gets a deep property's value from objects", () => { + eq(pathOr('Unknown', ['a', 'b', 'c'], deepObject), 'c') + eq(pathOr('Unknown', ['a'], deepObject), deepObject.a) + }) + + it('returns the default value for items not found', () => { + eq(pathOr('Unknown', ['a', 'b', 'foo'], deepObject), 'Unknown') + eq(pathOr('Unknown', ['bar'], deepObject), 'Unknown') + }) + + it('returns the default value for null/undefined', () => { + eq(pathOr('Unknown', ['toString'], null), 'Unknown') + eq(pathOr('Unknown', ['toString'], undefined), 'Unknown') + }) + + it('works with falsy items', () => { + eq(pathOr('Unknown', ['toString'], false), Boolean.prototype.toString) + }) +}) \ No newline at end of file diff --git a/specs/propOr.spec.ts b/specs/propOr.spec.ts new file mode 100644 index 0000000..016cd5b --- /dev/null +++ b/specs/propOr.spec.ts @@ -0,0 +1,31 @@ +import { describe, it } from "./_describe.ts" +import { propOr } from '../mod.ts' +import { eq } from "./utils/utils.ts" + + +describe('propOr', () => { + let shubham = {name: 'shubham', age: 23} + let shivam = {age: 99} + + let num = propOr('Unknown', 'name') + + it('should return a function that fetches the appropriate property', () => { + eq(typeof num, 'function') + eq(num(shubham), 'shubham') + }) + + it('should return the default value when the property does not exist', () => { + eq(num(shivam), 'Unknown') + }) + + it('should return the default value when the object is nil', () => { + eq(num(null), 'Unknown') + eq(num(void 0), 'Unknown') + }) + + it('should use the default when supplied an object with a nil value', () => { + eq(propOr('foo', 'x', {x: null}), 'foo') + eq(propOr('foo', 'x', {x: undefined}), 'foo') + }) + +}) \ No newline at end of file diff --git a/specs/propSatisfies.spec.ts b/specs/propSatisfies.spec.ts index cab5d5b..2254236 100644 --- a/specs/propSatisfies.spec.ts +++ b/specs/propSatisfies.spec.ts @@ -1,4 +1,4 @@ -import { describe, it, expect } from "./_describe.ts" +import { describe, it} from "./_describe.ts" import { propSatisfies } from '../mod.ts' import { eq } from "./utils/utils.ts" From 4f2ce62c7c6d66ef86a9b3e448e336f25983373d Mon Sep 17 00:00:00 2001 From: ch-shubham Date: Fri, 5 Jun 2020 11:23:43 +0530 Subject: [PATCH 5/7] fixing small bugs --- defaultTo.ts | 3 +-- eqProps.ts | 3 +-- propEq.ts | 2 +- propIs.ts | 12 ++++++------ propOr.ts | 2 +- propSatisfies.ts | 3 +-- specs/PropEq.spec.ts | 4 +++- specs/defaultTo.spec.ts | 1 + specs/eqProps.spec.ts | 8 +++++--- specs/pathOr.spec.ts | 12 +++++++----- specs/propIs.spec.ts | 11 +++++++---- specs/propOr.spec.ts | 1 + specs/propSatisfies.spec.ts | 4 +++- 13 files changed, 38 insertions(+), 28 deletions(-) diff --git a/defaultTo.ts b/defaultTo.ts index ac98ee2..1af12a4 100644 --- a/defaultTo.ts +++ b/defaultTo.ts @@ -5,7 +5,6 @@ function _defaultTo(defaultV: any, value: any) { return value == null || value !== value ? defaultV : value; } - /** * Returns the second argument if it is not `null`, `undefined` or `NaN`; * otherwise the first argument is returned. @@ -18,4 +17,4 @@ function _defaultTo(defaultV: any, value: any) { * defaultTo125(false) //=> false * defaultTo125('Fae') //=> 'Fae' */ -export const defaultTo: Curry2 = curryN(2, _defaultTo) +export const defaultTo: Curry2 = curryN(2, _defaultTo) \ No newline at end of file diff --git a/eqProps.ts b/eqProps.ts index 8491fbc..e9994bc 100644 --- a/eqProps.ts +++ b/eqProps.ts @@ -6,7 +6,6 @@ function _eqProps(prop: string, obj1: Obj, obj2: Obj) { return equals(obj1[prop], obj2[prop]) } - /** * Reports whether two objects have the same value, for the specified property. * Useful as a curried predicate. @@ -16,4 +15,4 @@ function _eqProps(prop: string, obj1: Obj, obj2: Obj) { * Fae.eqProps('a', obj1, obj2) //=> false * Fae.eqProps('c', obj1, obj2) //=> true */ -export const eqProps: Curry3 = curryN(3, _eqProps) +export const eqProps: Curry3 = curryN(3, _eqProps) \ No newline at end of file diff --git a/propEq.ts b/propEq.ts index 10e6a05..6f57da2 100644 --- a/propEq.ts +++ b/propEq.ts @@ -16,4 +16,4 @@ function _propEq(name: string, val: any, obj: ObjRec) { * const hasBrownHair = Fae.propEq('hair', 'brown') * Fae.filter(hasBrownHair, kids) //=> [shubham] */ -export const propEq: Curry3 = curryN(3, _propEq) +export const propEq: Curry3 = curryN(3, _propEq) \ No newline at end of file diff --git a/propIs.ts b/propIs.ts index cc23dff..051fdce 100644 --- a/propIs.ts +++ b/propIs.ts @@ -1,5 +1,5 @@ import curryN from "./utils/curry_n.ts" -import { Curry3, Func, Obj } from "./utils/types.ts" +import { Curry3, Obj } from "./utils/types.ts" import { is } from "./utils/is.ts"; function _propIs(type: string, name: string, obj: Obj) { @@ -7,11 +7,11 @@ function _propIs(type: string, name: string, obj: Obj) { } /** - * Returns `true` if the specified object property is of the given type; + * Returns `true` if the specified object property(must be passed as string) is of the given type; * `false` otherwise. * - * Fae.propIs(Number, 'a', {a: 1, y: 2}) //=> true - * Fae.propIs(String, 'a', {a: 'foo'}) //=> true - * Fae.propIs(Number, 'a', {}) //=> false + * Fae.propIs('Number', 'a', {a: 1, y: 2}); //=> true + * Fae.propIs('String', 'a', {a: 'foo'}); //=> true + * Fae.propIs('Number', 'a', {}); //=> false */ -export const propIs: Curry3 = curryN(3, _propIs) +export const propIs: Curry3 = curryN(3, _propIs) \ No newline at end of file diff --git a/propOr.ts b/propOr.ts index 2f4ef8b..876cf6c 100644 --- a/propOr.ts +++ b/propOr.ts @@ -20,4 +20,4 @@ function _propOr(val: any, p: string, obj: Obj | null) { * Great(Fae); //=> undefined * GreatWithDefault(Fae); //=> 'FaeModule' */ -export const propOr: Curry3 = curryN(3, _propOr) +export const propOr: Curry3 = curryN(3, _propOr) \ No newline at end of file diff --git a/propSatisfies.ts b/propSatisfies.ts index eeff529..828db3b 100644 --- a/propSatisfies.ts +++ b/propSatisfies.ts @@ -5,11 +5,10 @@ function _propSatisfies(pred: Predicate1, name: string, obj: ObjRec) { return pred(obj[name]); } - /** * Returns `true` if the specified object property satisfies the given * predicate; `false` otherwise. * * Fae.propSatisfies(x => x > 0, 'x', {x: 1, y: 2}); //=> true */ -export const propSatisfies: Curry3 = curryN(3, _propSatisfies) +export const propSatisfies: Curry3 = curryN(3, _propSatisfies) \ No newline at end of file diff --git a/specs/PropEq.spec.ts b/specs/PropEq.spec.ts index 2237b54..e38613f 100644 --- a/specs/PropEq.spec.ts +++ b/specs/PropEq.spec.ts @@ -1,8 +1,9 @@ -import { describe, it, expect } from "./_describe.ts" +import { describe, it } from "./_describe.ts" import { propEq } from '../mod.ts' import { eq } from "./utils/utils.ts" describe('propEq', () => { + let obj1 = {name: 'shubham', age: 22, hair: 'blue'} let obj2 = {name: 'Shivam', age: 21, hair: 'black'} @@ -11,4 +12,5 @@ describe('propEq', () => { eq(propEq('hair', 'black', obj2), true) eq(propEq('hair', 'blue', obj2), false) }) + }) \ No newline at end of file diff --git a/specs/defaultTo.spec.ts b/specs/defaultTo.spec.ts index fc84af9..21642c3 100644 --- a/specs/defaultTo.spec.ts +++ b/specs/defaultTo.spec.ts @@ -27,4 +27,5 @@ describe('defaultTo', () => { eq(125, defaultTo(125, null)) eq('a real value', defaultTo(125, 'a real value')) }) + }) \ No newline at end of file diff --git a/specs/eqProps.spec.ts b/specs/eqProps.spec.ts index f4ff0ea..77b5c42 100644 --- a/specs/eqProps.spec.ts +++ b/specs/eqProps.spec.ts @@ -1,13 +1,15 @@ -import { describe, it, expect } from "./_describe.ts" +import { describe, it } from "./_describe.ts" import { eqProps } from '../mod.ts' import { eq } from "./utils/utils.ts" -describe('propEq', () => { - it('should return true when two objects have the same value for a given property', function() { +describe('eqProps', () => { + + it('should return true when two objects have the same value for a given property', () => { eq(eqProps('name', {name: 'shubham', age: 10}, {name: 'shubham', age: 12}), true) eq(eqProps('name', {name: 'shivam', age: 10}, {name: 'shubham', age: 10}), false) eq(eqProps('value', {value: 0}, {value: -0}), false) eq(eqProps('value', {value: -0}, {value: 0}), false) eq(eqProps('value', {value: NaN}, {value: NaN}), true) }) + }) \ No newline at end of file diff --git a/specs/pathOr.spec.ts b/specs/pathOr.spec.ts index d592287..7ed1372 100644 --- a/specs/pathOr.spec.ts +++ b/specs/pathOr.spec.ts @@ -3,9 +3,10 @@ import { pathOr } from '../mod.ts' import { eq } from "./utils/utils.ts" describe('pathOr', () => { + let deepObject = {a: {b: {c: 'c'}}, falseVal: false, nullVal: null, undefinedVal: undefined, arrayVal: ['arr']} - it('takes a path and an object and returns the value at the path or the default value', () => { + it('should take a path and an object and returns the value at the path or the default value', () => { let obj = { a: { b: { @@ -29,22 +30,23 @@ describe('pathOr', () => { eq(pathOr('Unknown', ['a', 'b', 'c'], null), 'Unknown') }) - it("gets a deep property's value from objects", () => { + it("should get a deep property's value from objects", () => { eq(pathOr('Unknown', ['a', 'b', 'c'], deepObject), 'c') eq(pathOr('Unknown', ['a'], deepObject), deepObject.a) }) - it('returns the default value for items not found', () => { + it('should return the default value for items not found', () => { eq(pathOr('Unknown', ['a', 'b', 'foo'], deepObject), 'Unknown') eq(pathOr('Unknown', ['bar'], deepObject), 'Unknown') }) - it('returns the default value for null/undefined', () => { + it('should return the default value for null/undefined', () => { eq(pathOr('Unknown', ['toString'], null), 'Unknown') eq(pathOr('Unknown', ['toString'], undefined), 'Unknown') }) - it('works with falsy items', () => { + it('should work with falsy items', () => { eq(pathOr('Unknown', ['toString'], false), Boolean.prototype.toString) }) + }) \ No newline at end of file diff --git a/specs/propIs.spec.ts b/specs/propIs.spec.ts index b3ef370..c78af9d 100644 --- a/specs/propIs.spec.ts +++ b/specs/propIs.spec.ts @@ -2,13 +2,16 @@ import { describe, it } from "./_describe.ts" import { propIs } from '../mod.ts' import { eq } from "./utils/utils.ts" -describe('propEq', () => { +describe('propIs', () => { + it('should return true if the specified object property is of the given type', () => { - eq(propIs(Number, 'a', {a: 1, y: 2}), true) + eq(propIs('Number', 'a', {a: 1, y: 2}), true) + eq(propIs('String', 'a', {a: 'foo'}), true) }) it('Should return false otherwise', () => { - eq(propIs(String, 'ob', {ob: 1}), false) - eq(propIs(String, 'ob', {}), false) + eq(propIs('String', 'ob', {ob: 1}), false) + eq(propIs('String', 'ob', {}), false) }) + }) \ No newline at end of file diff --git a/specs/propOr.spec.ts b/specs/propOr.spec.ts index 016cd5b..337a2e2 100644 --- a/specs/propOr.spec.ts +++ b/specs/propOr.spec.ts @@ -4,6 +4,7 @@ import { eq } from "./utils/utils.ts" describe('propOr', () => { + let shubham = {name: 'shubham', age: 23} let shivam = {age: 99} diff --git a/specs/propSatisfies.spec.ts b/specs/propSatisfies.spec.ts index 2254236..5fb1b2e 100644 --- a/specs/propSatisfies.spec.ts +++ b/specs/propSatisfies.spec.ts @@ -2,7 +2,8 @@ import { describe, it} from "./_describe.ts" import { propSatisfies } from '../mod.ts' import { eq } from "./utils/utils.ts" -describe('propEq', () => { +describe('propSatisfies', () => { + let isPositive = (n: number) => n > 0 it('should return true if the specified object property satisfies the given predicate', () => { @@ -12,4 +13,5 @@ describe('propEq', () => { it('should return false otherwise', () => { eq(propSatisfies(isPositive, 'y', {x: 1, y: 0}), false) }) + }) \ No newline at end of file From c0258defb72b6fc6060e824892c43e623c29427f Mon Sep 17 00:00:00 2001 From: ch-shubham Date: Fri, 5 Jun 2020 18:59:44 +0530 Subject: [PATCH 6/7] working on review --- propEq.ts | 2 +- propOr.ts | 7 ++++--- specs/propIs.spec.ts | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/propEq.ts b/propEq.ts index 6f57da2..6237b76 100644 --- a/propEq.ts +++ b/propEq.ts @@ -14,6 +14,6 @@ function _propEq(name: string, val: any, obj: ObjRec) { * const Krish = {name: 'krish', age: 25, hair: 'black'} * const students = [abby, fred, rusty, alois] * const hasBrownHair = Fae.propEq('hair', 'brown') - * Fae.filter(hasBrownHair, kids) //=> [shubham] + * Fae.filter(hasBrownHair, students) //=> [shubham] */ export const propEq: Curry3 = curryN(3, _propEq) \ No newline at end of file diff --git a/propOr.ts b/propOr.ts index 876cf6c..895d6a4 100644 --- a/propOr.ts +++ b/propOr.ts @@ -1,8 +1,9 @@ import curryN from "./utils/curry_n.ts" -import { Curry3, Obj } from "./utils/types.ts" +import { Curry3, Obj, ObjRec } from "./utils/types.ts" import { pathOr } from "./pathOr.ts" +import { Path } from "./paths.ts" -function _propOr(val: any, p: string, obj: Obj | null) { +function _propOr(val: any, p: Path, obj: ObjRec | null) { return pathOr(val, [p], obj) } @@ -20,4 +21,4 @@ function _propOr(val: any, p: string, obj: Obj | null) { * Great(Fae); //=> undefined * GreatWithDefault(Fae); //=> 'FaeModule' */ -export const propOr: Curry3 = curryN(3, _propOr) \ No newline at end of file +export const propOr: Curry3 = curryN(3, _propOr) \ No newline at end of file diff --git a/specs/propIs.spec.ts b/specs/propIs.spec.ts index c78af9d..3983aaa 100644 --- a/specs/propIs.spec.ts +++ b/specs/propIs.spec.ts @@ -7,6 +7,7 @@ describe('propIs', () => { it('should return true if the specified object property is of the given type', () => { eq(propIs('Number', 'a', {a: 1, y: 2}), true) eq(propIs('String', 'a', {a: 'foo'}), true) + eq(propIs('Number', 'a', {}), false) }) it('Should return false otherwise', () => { From cee0ff553a2c2fe1c4e510078c7d6da069344816 Mon Sep 17 00:00:00 2001 From: ch-shubham Date: Fri, 5 Jun 2020 19:05:29 +0530 Subject: [PATCH 7/7] working on review-2 --- propEq.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/propEq.ts b/propEq.ts index 6237b76..a376c96 100644 --- a/propEq.ts +++ b/propEq.ts @@ -12,7 +12,7 @@ function _propEq(name: string, val: any, obj: ObjRec) { * const shivam = {name: 'shivam', age: 20, hair: 'brown'} * const shubham = {name: 'shubham', age: 22, hair: 'black'} * const Krish = {name: 'krish', age: 25, hair: 'black'} - * const students = [abby, fred, rusty, alois] + * const students = [shivam, shubham, krish] * const hasBrownHair = Fae.propEq('hair', 'brown') * Fae.filter(hasBrownHair, students) //=> [shubham] */