Skip to content

Commit

Permalink
Merge pull request #23 from monolithst/fix-type
Browse files Browse the repository at this point in the history
Fix type
  • Loading branch information
macornwell authored Apr 30, 2024
2 parents 66989b5 + 8b88a64 commit 4c63663
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ const Greetings = Model<Greeting>('Greetings', {
properties: {
name: TextProperty(),
greeting: TextProperty(),
displayName: DenormalizedProperty<string>("TextProperty", (modelData: TypedJsonObj<Greeting>) => {
displayName: DenormalizedProperty<string>("TextProperty", (modelData: Greeting) => {
return `${modelData.greeting} ${modelData.name}`
}),
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "functional-models",
"version": "2.1.6",
"version": "2.1.8",
"description": "A library for creating JavaScript function based models.",
"main": "index.js",
"types": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ type OptionalModelOptions<
type CalculateDenormalization<
T extends FunctionalValue,
TModel extends FunctionalModel,
> = (modelData: TypedJsonObj<TModel>) => MaybePromise<T>
> = (modelData: TModel, modelInstance: ModelInstance<TModel>) => MaybePromise<T>

export {
MaybeFunction,
Expand Down
81 changes: 74 additions & 7 deletions src/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,10 @@ const AdvancedModelReferenceProperty = <

/**
* A property for denormalizing values.
* @param propertyType
* @param calculate
* @param config
* @param additionalMetadata
* @param propertyType - A property type.
* @param calculate - A function for calculating the denormalized value.
* @param config - A Config
* @param additionalMetadata _ Any additional metadata.
* @constructor
*/
const DenormalizedProperty = <
Expand All @@ -479,12 +479,10 @@ const DenormalizedProperty = <
modelData: TModel,
modelInstance: ModelInstance<TModel, any>
) => {
console.log('I AM HERE')
console.log(value)
if (value !== undefined) {
return value
}
return calculate(modelData)
return calculate(modelData, modelInstance)
},
}),
additionalMetadata
Expand All @@ -494,6 +492,72 @@ const DenormalizedProperty = <
})
}

/**
* A Denormalized Property that is for text.
* @param calculate - A function that can get a string
* @param config - Any configs
* @param additionalMetadata - Optional Metadata
* @constructor
*/
const DenormalizedTextProperty = <T extends FunctionalModel>(
calculate: CalculateDenormalization<string, T>,
config: PropertyConfig<string> = {},
additionalMetadata = {}
) =>
DenormalizedProperty<string, T>(
PROPERTY_TYPES.TextProperty,
calculate,
merge(config, {
isString: true,
validators: mergeValidators(config, getCommonTextValidators(config)),
}),
additionalMetadata
)

/**
* A Denormalized Property that is for numbers.
* @param calculate - A function that can get a string
* @param config - Any configs
* @param additionalMetadata - Optional Metadata
* @constructor
*/
const DenormalizedNumberProperty = <T extends FunctionalModel>(
calculate: CalculateDenormalization<number, T>,
config: PropertyConfig<number> = {},
additionalMetadata = {}
) =>
DenormalizedProperty<number, T>(
PROPERTY_TYPES.NumberProperty,
calculate,
merge(config, {
isNumber: true,
validators: mergeValidators(config, getCommonNumberValidators(config)),
}),
additionalMetadata
)

/**
* A Denormalized Property that is for integers.
* @param calculate - A function that can get a string
* @param config - Any configs
* @param additionalMetadata - Optional Metadata
* @constructor
*/
const DenormalizedIntegerProperty = <T extends FunctionalModel>(
calculate: CalculateDenormalization<number, T>,
config: PropertyConfig<number> = {},
additionalMetadata = {}
) =>
DenormalizedProperty<number, T>(
PROPERTY_TYPES.IntegerProperty,
calculate,
merge(config, {
isInteger: true,
validators: mergeValidators(config, getCommonNumberValidators(config)),
}),
additionalMetadata
)

/**
* An Id that is naturally formed by the other properties within a model.
* Instead of having a "globally unique" id the model is unique because
Expand Down Expand Up @@ -564,4 +628,7 @@ export {
EmailProperty,
BooleanProperty,
DenormalizedProperty,
DenormalizedIntegerProperty,
DenormalizedNumberProperty,
DenormalizedTextProperty,
}
97 changes: 95 additions & 2 deletions test/src/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import {
EmailProperty,
NaturalIdProperty,
DenormalizedProperty,
DenormalizedIntegerProperty,
DenormalizedNumberProperty,
DenormalizedTextProperty,
} from '../../src/properties'
import { TYPE_PRIMITIVES, arrayType } from '../../src/validation'
import { BaseModel } from '../../src/models'
Expand Down Expand Up @@ -49,6 +52,96 @@ const TestModel1 = BaseModel<TestModelType>('TestModel1', {
})

describe('/src/properties.ts', () => {
describe('#DenormalizedTextProperty()', () => {
it('should return "Hello Dolly"', async () => {
type Greeting = {
name: string
greeting: string
displayName?: string
}

const displayNameProperty = DenormalizedTextProperty<Greeting>(
(modelData: TypedJsonObj<Greeting>) => {
return `${modelData.greeting} ${modelData.name}`
}
)

const getter = displayNameProperty.createGetter(
// @ts-ignore
undefined,
{
name: 'Dolly',
greeting: 'Hello',
displayName: undefined,
},
{}
)

const actual = await getter()
const expected = 'Hello Dolly'
assert.deepEqual(actual, expected)
})
})
describe('#DenormalizedNumberProperty()', () => {
it('should return 123.456', async () => {
type MyType = {
x: number
y: number
calculated?: number
}

const displayNameProperty = DenormalizedNumberProperty<MyType>(
(modelData: MyType) => {
return modelData.x + modelData.y
}
)

const getter = displayNameProperty.createGetter(
// @ts-ignore
undefined,
{
x: 123.0,
y: 0.456,
calculated: undefined,
},
{}
)

const actual = await getter()
const expected = 123.456
assert.deepEqual(actual, expected)
})
})
describe('#DenormalizedIntegerProperty()', () => {
it('should return 555', async () => {
type MyType = {
x: number
y: number
calculated?: number
}

const displayNameProperty = DenormalizedIntegerProperty<MyType>(
(modelData: MyType) => {
return modelData.x + modelData.y
}
)

const getter = displayNameProperty.createGetter(
// @ts-ignore
undefined,
{
x: 222,
y: 333,
calculated: undefined,
},
{}
)

const actual = await getter()
const expected = 555
assert.deepEqual(actual, expected)
})
})
describe('#DenormalizedProperty()', () => {
it('should return "Hello Dolly"', async () => {
type Greeting = {
Expand All @@ -59,7 +152,7 @@ describe('/src/properties.ts', () => {

const displayNameProperty = DenormalizedProperty<string, Greeting>(
'TextProperty',
(modelData: TypedJsonObj<Greeting>) => {
(modelData: Greeting) => {
return `${modelData.greeting} ${modelData.name}`
}
)
Expand Down Expand Up @@ -88,7 +181,7 @@ describe('/src/properties.ts', () => {

const displayNameProperty = DenormalizedProperty<string, Greeting>(
'TextProperty',
(modelData: TypedJsonObj<Greeting>) => {
(modelData: Greeting) => {
return `${modelData.greeting} ${modelData.name}`
}
)
Expand Down

0 comments on commit 4c63663

Please sign in to comment.