Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ------------------------------------------------------------------- */
- /**
- * Parses data and corrects types: from string to boolean | number
- *
- * @param data - Data to parse
- */
- /* ------------------------------------------------------------------- */
- export const parseTypes = (data: any) => {
- let result: any = { };
- // Parse null
- if (data === null)
- result = null;
- // Parse string
- else if (typeof data === 'string')
- if (data.toLowerCase() === 'true')
- result = true;
- else if (data.toLowerCase() === 'false')
- result = false;
- else if (!isNaN(+data))
- result = +data;
- else
- result = data;
- // Parse array
- else if (Array.isArray(data))
- result = data.map(item => parseTypes(item));
- // Parse object
- else if (typeof data === 'object')
- for (const key of Object.keys(data))
- result[key] = parseTypes(data[key]);
- // Else
- else
- result = data;
- return result;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement