diff --git a/exercises b/exercises index 4ff8f9c..28be2c5 100644 --- a/exercises +++ b/exercises @@ -24,7 +24,9 @@ take the following input data "name": "Gill", "type": "fish" } -]1. and group them by type +] + +1. and group them by type ex: [ { @@ -35,9 +37,15 @@ ex: type: dog, animals: ['Manuel', 'Sparky', 'John'] }, etc... -]2. Given 2-3 animals that are in the list create an interesction of the list. -ex: given manuel and gill, intersect with the list of animals. Some animals may NOT appear in both lists, handle that case!3. Given an array, generate a CSV representation of that array with a set of headers. +] + +2. Given 2-3 animals that are in the list create an interesction of the list. +ex: given manuel and gill, intersect with the list of animals. Some animals may NOT appear in both lists, handle that case! + +3. Given an array, generate a CSV representation of that array with a set of headers. ex: NAME, TYPE Gill, fish -Manuel, dog4. Create a function that can only run once and throws if it gets called again.5. Create a function that is given a set of functions and a set of data which are inputs for those functions and returns the one that finished the fastest out of them, displaying ms time \ No newline at end of file +Manuel, dog + +4. Create a function that can only run once and throws if it gets called again.5. Create a function that is given a set of functions and a set of data which are inputs for those functions and returns the one that finished the fastest out of them, displaying ms time \ No newline at end of file diff --git a/group.ts b/group.ts index 08d32bb..38a6ce0 100644 --- a/group.ts +++ b/group.ts @@ -1,9 +1,9 @@ interface DataEntry { - name: 'John' | 'Sparky' | 'Nova' | 'Manuel' | 'Caesar' | 'Gill'; - type: 'dog' | 'cat' | 'fish'; + name: 'John' | 'Sparky' | 'Nova' | 'Manuel' | 'Caesar' | 'Gill' | 'Billy' | 'Silly'; + type: 'dog' | 'cat' | 'fish' | 'bug' | 'bird'; }; -const data:Array = [ +const data1: Array = [ { "name": "John", "type": "dog" @@ -30,11 +30,30 @@ const data:Array = [ } ]; +const data2: Array = [ + { + "name": "John", + "type": "dog" + }, + { + "name": "Sparky", + "type": "dog" + }, + { + "name": "Silly", + "type": "bug" + }, + { + "name": "Billy", + "type": "bird" + } +] + interface OutputEntry { type: string; animals: Array; } - + const groupByType = (ar: Array) => { return ar.reduce((acc:Array, element:DataEntry): Array => { @@ -51,5 +70,44 @@ const groupByType = (ar: Array) => { }, [] as Array); } -const elements = groupByType(data); -console.log(elements); \ No newline at end of file +// 1 - group by type +const elements = groupByType(data1); +console.log(elements); + +// 2 - data intersection +const intersection = (ar1: Array, ar2: Array): Array => { + return ar1.reduce((acc: Array, el1: DataEntry): Array => { + const result: DataEntry | void = ar2.find((el2: DataEntry): any => el2.name === el1.name && el2.type === el1.type); + if (result) { + acc.push(result); + } + + return acc; + }, [] as Array); +}; + +console.log( intersection(data1, data2)); + +// 3 - generate a csv +const CSVFormater = (content: Array>): string => { + const result = content.map((line: Array): string => line.join(',')); + + return result.join('\n') +}; + +const CSVGeneartor = (ar: Array): string => { + if (ar.length === 0) { + return ''; + } + + const header = Object.keys(ar[0]); + const content = []; + content.push(header); + ar.forEach((el: DataEntry): void => { + content.push(Object.values(el)); + }); + + return CSVFormater(content); +} + +console.log( '---', CSVGeneartor(data1)); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..48e341a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +} diff --git a/tsconfig.json b/tsconfig.json index b2b55da..e54d828 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ - "target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ + "target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */