Skip to content

Commit 34573b1

Browse files
authored
Merge pull request #17 from suyashvash/main
Added Sanitize array function
2 parents 0353d83 + db83324 commit 34573b1

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
@@ -179,6 +179,60 @@ function get_element_by_key_value(arr, key, value) {
179179
return arr.filter((item) => item[key] == value);
180180
}
181181

182+
/**
183+
* To sanitize the array of objects with respect to the given schema
184+
*
185+
* See exmaple in Readme for better understanding....
186+
*
187+
* @param array arr
188+
* @param object schema { "keyName":"dataType" }
189+
* @return array
190+
*
191+
*/
192+
193+
function sanitize_array(arr,schema){
194+
let all_data_types = [];
195+
let all_keys = [];
196+
197+
const schema_array = Object.entries(schema);
198+
schema_array.forEach((type) => all_data_types.push(type[1]));
199+
200+
schema_array.forEach((key) => all_keys.push(key[0]));
201+
202+
arr.forEach((element)=>{
203+
all_keys.forEach((key,key_index)=>{
204+
if(`${typeof(element[all_keys[key_index]])}`!==`${all_data_types[key_index]}`){
205+
if(all_data_types[key_index]=='number'){
206+
if(!isNaN(parseInt(element[all_keys[key_index]]))){
207+
element[all_keys[key_index]] = parseInt(element[all_keys[key_index]]);
208+
}else{
209+
element[all_keys[key_index]] = 0;
210+
}
211+
}else if(all_data_types[key_index]=='string'){
212+
element[all_keys[key_index]] = `${element[all_keys[key_index]]}`;
213+
}else if(all_data_types[key_index]=='boolean'){
214+
if( element[all_keys[key_index]]=='true'){
215+
element[all_keys[key_index]] = true;
216+
}else if(element[all_keys[key_index]]=='false'){
217+
element[all_keys[key_index]] = false;
218+
}else if(element[all_keys[key_index]]==null){
219+
element[all_keys[key_index]] = false;
220+
}else{
221+
element[all_keys[key_index]] = true;
222+
}
223+
}else{
224+
element[all_keys[key_index]] = null;
225+
}
226+
227+
}
228+
})
229+
230+
231+
})
232+
233+
return arr
234+
}
235+
182236
export {
183237
is_array,
184238
is_num_array,
@@ -195,4 +249,5 @@ export {
195249
array_slice_sum,
196250
sort_nums,
197251
get_element_by_key_value,
252+
sanitize_array,
198253
};

0 commit comments

Comments
 (0)