Skip to content

Commit

Permalink
issue-#57 - Progress on idiomatic curry - Converted methods up to './…
Browse files Browse the repository at this point in the history
…src/object/defineProp'.
  • Loading branch information
elycruz committed Aug 9, 2022
1 parent cd076ea commit f8bbffe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
6 changes: 4 additions & 2 deletions packages/fjl/src/object/assignDeep.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {isType} from './is';
import {curry2} from '../function/curry';

export const

Expand All @@ -25,4 +24,7 @@ export const
}, topAgg)
, obj0);

export const $assignDeep = curry2(assignDeep);
export const $assignDeep = (obj0) =>
(...objs) => assignDeep(obj0, objs)

;
27 changes: 19 additions & 8 deletions packages/fjl/src/object/defineProp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {curry2, curry3} from '../function/curry';
import {errorIfNotType} from '../errorThrowing';
import {isUndefined} from './is';
import {TypeRef} from "../types";
Expand Down Expand Up @@ -48,39 +47,51 @@ export const
return [_target, descriptor];
},

$defineProp = curry3(defineProp),
$defineProp = <T>(Type: TypeRef) =>
(target: T | [T, PropertyDescriptor?]) =>
(propName: string, defaultValue?: any): [T, PropertyDescriptor] =>
defineProp(Type, target, propName, defaultValue),

/**
* Allows you to define a "typed", enumerated property on `target`.
*/
defineEnumProp = <T>(
Type: TypeRef, target: T | [T, PropertyDescriptor?], propName: string, defaultValue?: any
Type: TypeRef, target: T | [T, PropertyDescriptor?],
propName: string,
defaultValue?: any
): [T, PropertyDescriptor] => {
const [_target, _descriptor] = toTargetDescriptorTuple(target),
descriptor = _descriptor || createTypedDescriptor(Type, _target, propName);
return $defineProp(
return defineProp(
Type,
toEnumerableDescriptor([_target, descriptor]),
propName,
defaultValue
) as unknown as [T, PropertyDescriptor];
},

$defineEnumProp = curry3(defineEnumProp),
$defineEnumProp = <T>(Type: TypeRef) =>
(target: T | [T, PropertyDescriptor?]) =>
(propName: string, defaultValue?: any): [T, PropertyDescriptor] =>
defineEnumProp(Type, target, propName, defaultValue),

/**
* Allows you to define multiple enum props at once on target.
*/
defineEnumProps = createDefinePropsMethod({enumerable: true}),

$defineEnumProps = curry2(defineEnumProps),
$defineEnumProps = <T>(argsTuple: [TypeRef, string, any?][]) =>
(target: T) =>
defineEnumProps(argsTuple, target),

/**
* Allows you to define multiple props at once on target.
*/
defineProps = createDefinePropsMethod({enumerable: false}),

$defineProps = curry2(defineProps)
$defineProps = <T>(argsTuple: [TypeRef, string, any?][]) =>
(target: T) =>
defineProps(argsTuple, target)

;

Expand All @@ -90,7 +101,7 @@ export type PropsDefiner = <T>(argsTuple: [TypeRef, string, any?][], target: T)
* Creates `defineProps` and `defineEnumProps` methods based on `{enumerable}` param.
*/
function createDefinePropsMethod({enumerable}: PropertyDescriptor): PropsDefiner {
const operation = enumerable ? $defineEnumProp : $defineProp;
const operation = enumerable ? defineEnumProp : defineProp;
return <T>(argsTuple: [TypeRef, string, any?][], target: T): T => {
argsTuple.forEach(argTuple => {
const [TypeRef, propName, defaultValue] = argTuple;
Expand Down
1 change: 1 addition & 0 deletions packages/fjl/tests/object/index_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,7 @@ describe('#object', function () {
generateTargetData().forEach((args: Parameters<typeof defineEnumProps>) => {
// log(args);
const target = defineEnumProps(...args),
// @ts-ignore
propNames = args[0].map(([_, name]) => name);
expect(target).toBeInstanceOf(Object);
propNames.forEach(name => {
Expand Down

0 comments on commit f8bbffe

Please sign in to comment.