cakenull
Version:
null is not scary
74 lines (58 loc) • 2.15 kB
Markdown
## nullCake
1. check if the data is null or undefined
### example
```javascript
import nullCake from "nullCake";
const myDataCheck = nullCake();
//getType
const myDataType = myDataCheck.getType(data);
//check data
const isNull = myDataCheck.check(data);
// translate data to targetType
const myDataNew = myDataCheck.setData(datatypeObj, dataObj);
```
### methods
| Method | Description |
| ------- | ---------------------------------------------- |
| getType | get data type ,return data's type in lowerCase |
| check | check if the data is null or undefined |
| setData | translate data to targetType |
#### use methods
1. getType
- get data type ,return data's type in lowerCase
```javascript
const data = "get my data type";
const type = nullCake.getType(data);
console.log({ type }); // type: 'string'
```
2. check
- check if the data is null or undefined
```javascript
const data = null
const isNull = nullCake.check(data)
console.log({isNull}) // isNull: true
--------------------------------------------------------
const dataString = 'null' | 'Null' |'NULL'
const isNull = nullCake.check(dataString)
console.log({isNull}) // isNull: 'null string'
--------------------------------------------------------
const dataUndefined = undefined
const isNull = nullCake.check(dataUndefined)
console.log({isNull}) // isNull: 'undefined'
--------------------------------------------------------
const undefinedStr = 'undefined' | 'Undefined' |'UNDEFINED'
const isNull = nullCake.check(undefinedStr)
console.log({isNull}) // isNull: 'undefined string'
--------------------------------------------------------
const data = 'string' | [] | ...
const isNull = nullCake.check(data)
console.log({isNull}) // isNull: false
```
3. setData
- translate data to targetType
```javascript
const targetData = { a: "", b: [], c: 0, d: () => {} };
const orginData = { a: "had a value", b: null, d: undefined };
const tData = nullCake.setData(targetData, orginData);
console.log({ tData }); // tData: {a: 'had a value', b:[], c:0, d:()=>{}}
```