Skip to content

Commit 0d287c9

Browse files
authored
Merge branch 'main' into main
2 parents 4715cf6 + 34573b1 commit 0d287c9

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In your `package.json` add the following, `"type": "module"`.
2020
# Example Usage
2121

2222
```js
23-
import { is_array, object_to_array, search_in_array } from "@hetarth02/js-array-helpers";
23+
import { is_array, object_to_array, search_in_array,sanitize_array } from "@hetarth02/js-array-helpers";
2424

2525
let arr = [1, 2];
2626
console.log(is_array(arr)); // true
@@ -35,4 +35,45 @@ console.log(object_to_array(objectX)); // ['Apple', 'Microsoft', 'Google']
3535
const mang = ['Microsoft', 'apple', 'netflix', 'Google'];
3636
const result = search_in_array("app", mang);
3737
console.log(result); // ['apple']
38+
39+
40+
41+
42+
43+
44+
// Santized array Example
45+
46+
// Corrupted Data array with diff data types
47+
const my_array = [
48+
{name:'sam', age:null, isEmployed:'false'},
49+
{name:'a', age:456, isEmployed:false},
50+
{name:'c', age:undefined, isEmployed:00} ,
51+
{name:null, age:123, isEmployed:true} ,
52+
{name:'asd', age:123, isEmployed:false} ,
53+
{name:00, age:123, isEmployed:null} ,
54+
{name:'sam', age:'123', isEmployed:undefined}
55+
]
56+
57+
58+
59+
// Given schema for correct data types
60+
const my_schema = {
61+
"name":'string',
62+
"age":'number',
63+
"isEmployed":'boolean'
64+
}
65+
66+
// Run sanitize_array with array and schema
67+
console.log(sanitize_array(my_array,my_schema))
68+
69+
// Sanitized Output
70+
// [ { name: 'sam', age: 0, isEmployed: false },
71+
// { name: 'a', age: 456, isEmployed: false },
72+
// { name: 'c', age: 0, isEmployed: true },
73+
// { name: 'null', age: 123, isEmployed: true },
74+
// { name: 'asd', age: 123, isEmployed: false },
75+
// { name: '0', age: 123, isEmployed: false },
76+
// { name: 'sam', age: 123, isEmployed: false }
77+
// ]
78+
3879
```

index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,60 @@ function difference(arr1, arr2) {
223223
return arr1.filter((item) => !arr2.includes(item));
224224
}
225225

226+
/**
227+
* To sanitize the array of objects with respect to the given schema
228+
*
229+
* See exmaple in Readme for better understanding....
230+
*
231+
* @param array arr
232+
* @param object schema { "keyName":"dataType" }
233+
* @return array
234+
*
235+
*/
236+
237+
function sanitize_array(arr,schema){
238+
let all_data_types = [];
239+
let all_keys = [];
240+
241+
const schema_array = Object.entries(schema);
242+
schema_array.forEach((type) => all_data_types.push(type[1]));
243+
244+
schema_array.forEach((key) => all_keys.push(key[0]));
245+
246+
arr.forEach((element)=>{
247+
all_keys.forEach((key,key_index)=>{
248+
if(`${typeof(element[all_keys[key_index]])}`!==`${all_data_types[key_index]}`){
249+
if(all_data_types[key_index]=='number'){
250+
if(!isNaN(parseInt(element[all_keys[key_index]]))){
251+
element[all_keys[key_index]] = parseInt(element[all_keys[key_index]]);
252+
}else{
253+
element[all_keys[key_index]] = 0;
254+
}
255+
}else if(all_data_types[key_index]=='string'){
256+
element[all_keys[key_index]] = `${element[all_keys[key_index]]}`;
257+
}else if(all_data_types[key_index]=='boolean'){
258+
if( element[all_keys[key_index]]=='true'){
259+
element[all_keys[key_index]] = true;
260+
}else if(element[all_keys[key_index]]=='false'){
261+
element[all_keys[key_index]] = false;
262+
}else if(element[all_keys[key_index]]==null){
263+
element[all_keys[key_index]] = false;
264+
}else{
265+
element[all_keys[key_index]] = true;
266+
}
267+
}else{
268+
element[all_keys[key_index]] = null;
269+
}
270+
271+
}
272+
})
273+
274+
275+
})
276+
277+
return arr
278+
}
279+
226280
export {
227281
is_array,
228282
is_num_array,
@@ -242,4 +296,5 @@ export {
242296
flatten,
243297
intersection,
244298
difference,
299+
sanitize_array,
245300
};

0 commit comments

Comments
 (0)