@sumor/validator
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "@sumor/validator", | ||
"description": "This is a lightweight validator for Node.JS. It can validate the input string or number based on the rules you defined.", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "repository": "sumor-cloud/validator", |
@@ -156,2 +156,6 @@ # validator | ||
##### Trim Usage | ||
will remove the useless space for prefix and suffix | ||
```js | ||
@@ -161,9 +165,42 @@ import { format } from '@sumor/validator' | ||
const parameterInfo = { | ||
type: 'string' | ||
type: 'string', | ||
trim: true // default is true for string type | ||
} | ||
const value1 = format(parameterInfo, ' demo ') | ||
console.log(value1) // will print "demo", useless space will be removed | ||
const value = format(parameterInfo, ' demo ') | ||
console.log(value) // will print "demo", useless space will be removed | ||
``` | ||
##### Lowercase Usage | ||
will convert the string to lowercase | ||
```js | ||
import { format } from '@sumor/validator' | ||
const parameterInfo = { | ||
type: 'string', | ||
lowercase: true | ||
} | ||
const value = format(parameterInfo, 'Demo') | ||
console.log(value) // will print "demo", all characters will be converted to lowercase | ||
``` | ||
##### Uppercase Usage | ||
will convert the string to uppercase | ||
```js | ||
import { format } from '@sumor/validator' | ||
const parameterInfo = { | ||
type: 'string', | ||
uppercase: true | ||
} | ||
const value = format(parameterInfo, 'Demo') | ||
console.log(value) // will print "DEMO", all characters will be converted to uppercase | ||
``` | ||
### Format Number Usage | ||
@@ -170,0 +207,0 @@ |
export default (info, value) => { | ||
if (value === undefined || value === null || value === '') { | ||
return null | ||
if (value === undefined || value === '') { | ||
value = null | ||
} | ||
// default value | ||
if (value === null) { | ||
if (info.default) { | ||
value = info.default | ||
} else { | ||
return null | ||
} | ||
} | ||
// convert to number | ||
if (typeof value !== 'number') { | ||
@@ -7,0 +17,0 @@ value = parseFloat(value) |
export default (info, value) => { | ||
if (info.trim) { | ||
value = value.trim() | ||
if (value === undefined) { | ||
value = null | ||
} | ||
// convert to string | ||
if (value !== null && typeof value !== 'string') { | ||
if (typeof value === 'object') { | ||
value = JSON.stringify(value) | ||
} else { | ||
value = value.toString() | ||
} | ||
} | ||
// default value | ||
if (info.default !== undefined) { | ||
if (value === null || value === '') { | ||
value = info.default | ||
} | ||
} | ||
if (value !== null) { | ||
// trim value | ||
if (info.trim) { | ||
value = value.trim() | ||
} | ||
// lowerCase value | ||
if (info.lowerCase) { | ||
value = value.toLowerCase() | ||
} | ||
// upperCase value | ||
if (info.upperCase) { | ||
value = value.toUpperCase() | ||
} | ||
} | ||
return value | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16594
275
220