Skip to content

Commit

Permalink
+ Add ParseInt & ParseFloat.
Browse files Browse the repository at this point in the history
  • Loading branch information
LancerComet committed Feb 4, 2024
1 parent f53658a commit 8966b39
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 48 deletions.
25 changes: 25 additions & 0 deletions packages/suntori/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ const json = serialize(doge) // json should be "equal" to dataSource.

## More decorators

There are also some additional decorators, providing extra functionalities.

### @JsonString

```ts
Expand Down Expand Up @@ -223,6 +225,29 @@ console.log(a.numNullable) // null
console.log(a.numNullable2) // 0, only got null if payload were null.
```

### @ParseInt and @ParseFloat

```ts
@Serializable()
class A {
@JsonProperty()
@ParseInt()
readonly int: number = 0

@JsonProperty()
@ParseFloat()
readonly float: number = 0
}

const a = deserialize({
int: '10',
float: '0.1'
}, A)

console.log(a.int) // 10
console.log(a.float) // 0.1
```

## License

Apache-2.0
4 changes: 4 additions & 0 deletions packages/suntori/lib/config/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const META_KEY_SERIALIZABLE = 'suntori:serializable'
const META_KEY_JSON_STRING = 'suntori:jsonString'
const META_KEY_DYNAMIC_KEY = 'suntori:dynamicKey'
const META_KEY_NULLABLE = 'suntori:nullable'
const META_KEY_PARSE_INT = 'suntori:parseInt'
const META_KEY_PARSE_FLOAT = 'suntori:parseFloat'
const META_KEY_JSON_IGNORE = 'suntori:jsonIgnore'

export {
Expand All @@ -11,5 +13,7 @@ export {
META_KEY_JSON_STRING,
META_KEY_DYNAMIC_KEY,
META_KEY_NULLABLE,
META_KEY_PARSE_INT,
META_KEY_PARSE_FLOAT,
META_KEY_JSON_IGNORE
}
42 changes: 41 additions & 1 deletion packages/suntori/lib/deserialize/decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { META_KEY_NULLABLE, META_KEY_DYNAMIC_KEY, META_KEY_JSON_STRING } from '../config/meta'
import { META_KEY_NULLABLE, META_KEY_DYNAMIC_KEY, META_KEY_JSON_STRING, META_KEY_PARSE_INT, META_KEY_PARSE_FLOAT } from '../config/meta'
import { createPlainObject } from '../utils/object'

/**
Expand Down Expand Up @@ -113,6 +113,44 @@ function Nullable () {
return defineDecorator(META_KEY_NULLABLE)
}

/**
* A prop that decorated by this decorator will be parsed as int in force.
*
* @example
* @Serializable()
* class A {
* @JsonProperty()
* @ParseInt()
* readonly id: number = 0
* }
*
* const a = deserialize({
* id: '10'
* }, A)
*/
function ParseInt () {
return defineDecorator(META_KEY_PARSE_INT)
}

/**
* A prop that decorated by this decorator will be parsed as float in force.
*
* @example
* @Serializable()
* class A {
* @JsonProperty()
* @ParseFloat()
* readonly value: number = 0
* }
*
* const a = deserialize({
* value: '10.24'
* }, A)
*/
function ParseFloat () {
return defineDecorator(META_KEY_PARSE_FLOAT)
}

/**
* Check a meta registry has target prop assigned.
*
Expand All @@ -129,5 +167,7 @@ export {
JsonString,
DynamicKey,
Nullable,
ParseInt,
ParseFloat,
checkMetaRegistryHasProp
}
19 changes: 18 additions & 1 deletion packages/suntori/lib/deserialize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
META_KEY_DYNAMIC_KEY,
META_KEY_JSON_PROPERTY,
META_KEY_JSON_STRING,
META_KEY_SERIALIZABLE
META_KEY_SERIALIZABLE,
META_KEY_PARSE_INT,
META_KEY_PARSE_FLOAT
} from '../config/meta'
import { ConstructorOf, IAllPropertiesMetaData, IJsonPropertyOption } from '../types'
import { checkIsSerializable } from '../utils/meta'
Expand Down Expand Up @@ -127,6 +129,21 @@ function createModelValueFromJson (
}

if (expectedType === Number) {
const doIntParse = checkMetaRegistryHasProp(META_KEY_PARSE_INT, propName, instance)
const doFloatParse = checkMetaRegistryHasProp(META_KEY_PARSE_FLOAT, propName, instance)

if (isString(payload)) {
if (doIntParse) {
const parsedValue = parseInt(payload)
return isNaN(parsedValue) ? fallbackValue : parsedValue
}

if (doFloatParse) {
const parsedValue = parseFloat(payload)
return isNaN(parsedValue) ? fallbackValue : parsedValue
}
}

return isNumber(payload) ? payload : fallbackValue
}

Expand Down
2 changes: 1 addition & 1 deletion packages/suntori/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { deserialize, Serializable, JsonProperty } from './deserialize'
export { JsonString, DynamicKey, Nullable } from './deserialize/decorator'
export { JsonString, DynamicKey, Nullable, ParseInt, ParseFloat } from './deserialize/decorator'
export { serialize, JsonIgnore } from './serialize'
export { cloneModel } from './utils/clone-model'
Loading

0 comments on commit 8966b39

Please sign in to comment.