From 55d50da1b3a22295d75d0036ad43f328265683cf Mon Sep 17 00:00:00 2001 From: Ueli Kunz Date: Fri, 1 May 2020 19:11:49 +0200 Subject: [PATCH] update es6 modules --- src/exercises/modules/admin.js | 22 ++++++++++++------- .../solutions/modules/admin/admin.module.js | 5 ++--- .../solutions/modules/admin/client.js | 8 +++---- .../solutions/modules/admin/index.html | 11 ++++++++++ .../solutions/modules/admin/person.js | 5 ++--- .../solutions/modules/admin/store.js | 19 ++++++++++++---- src/modules/app.js | 14 ++++++++++++ src/modules/index.html | 11 ++++++++++ src/modules/logger.js | 14 ------------ src/modules/logger.module.js | 20 +++++++++++++++++ src/modules/sample.js | 13 ----------- src/oop-es6.js | 3 ++- 12 files changed, 95 insertions(+), 50 deletions(-) create mode 100644 src/exercises/solutions/modules/admin/index.html create mode 100644 src/modules/app.js create mode 100644 src/modules/index.html delete mode 100644 src/modules/logger.js create mode 100644 src/modules/logger.module.js delete mode 100644 src/modules/sample.js diff --git a/src/exercises/modules/admin.js b/src/exercises/modules/admin.js index 1eac70c..c667276 100644 --- a/src/exercises/modules/admin.js +++ b/src/exercises/modules/admin.js @@ -1,16 +1,22 @@ -/* +/* Erstelle ein Admin Modul, dass -- eine Klasse Person +- eine Klasse Person - und eine Klasse Store zur Verfügung stellt. -Interfaces: +So sollen die Klassen aussehen: + Person(name, vorname) + Store(items) - - add(item) - - remove(item) - - findAll(item=>boolean) + // items kann z.B. ein Array von Personen sein + - add(item) + - remove(item) + - findAll(item => boolean) + // das bedeutet: die Methode findAll erwartet als Argument eine Funktion, + welche als Argument ein Item entgegen nimmt und ein Boolean retourniert -Schreibe ein Programm (client.js), welches einen Store verwendet um Personen darin zu speichern, löschen, suchen. -*/ \ No newline at end of file +Schreibe ein Programm client.js, welches einen Store verwendet um Personen darin +zu speichern, löschen, suchen. +*/ diff --git a/src/exercises/solutions/modules/admin/admin.module.js b/src/exercises/solutions/modules/admin/admin.module.js index 2b1dc77..1c16cfe 100644 --- a/src/exercises/solutions/modules/admin/admin.module.js +++ b/src/exercises/solutions/modules/admin/admin.module.js @@ -1,4 +1,3 @@ -'use strict' -export {default as Person} from './person' -export {default as Store} from './store' \ No newline at end of file +export {Person} from './person.js' +export {Store} from './store.js' diff --git a/src/exercises/solutions/modules/admin/client.js b/src/exercises/solutions/modules/admin/client.js index c5e46fc..f47cb1f 100644 --- a/src/exercises/solutions/modules/admin/client.js +++ b/src/exercises/solutions/modules/admin/client.js @@ -1,5 +1,4 @@ -'use strict' -import {Person, Store} from './admin.module' +import {Person, Store} from './admin.module.js' let p1 = new Person('Agatha', 'Kurnikova') @@ -9,12 +8,13 @@ store.add(p1) store.add({ name: 'Hans', vorname: 'Meier' }) +console.table(store.items) store.remove(p1) let pWithHa = store.findAll((p) => p.name.startsWith('Ha')) -pWithHa +console.log('starts with Ha:', pWithHa[0]) store.remove(pWithHa.pop()) -store \ No newline at end of file +console.log(store.items.length) diff --git a/src/exercises/solutions/modules/admin/index.html b/src/exercises/solutions/modules/admin/index.html new file mode 100644 index 0000000..dfb9a41 --- /dev/null +++ b/src/exercises/solutions/modules/admin/index.html @@ -0,0 +1,11 @@ + + + + + Store Client - Modules + + + + + + diff --git a/src/exercises/solutions/modules/admin/person.js b/src/exercises/solutions/modules/admin/person.js index 4502530..b5daa2e 100644 --- a/src/exercises/solutions/modules/admin/person.js +++ b/src/exercises/solutions/modules/admin/person.js @@ -1,7 +1,6 @@ -'use strict' -export default class { +export class Person { constructor(name, vorname){ this.name = name this.vorname = vorname } -} \ No newline at end of file +} diff --git a/src/exercises/solutions/modules/admin/store.js b/src/exercises/solutions/modules/admin/store.js index ac4a6d8..79b3dde 100644 --- a/src/exercises/solutions/modules/admin/store.js +++ b/src/exercises/solutions/modules/admin/store.js @@ -1,18 +1,29 @@ -'use strict' -export default class { +export class Store { constructor(items){ this.items = items || [] } + add(item){ this.items.push(item) } + remove(item){ let itemIdx = this.items.indexOf(item) if(itemIdx > -1){ this.items.splice(itemIdx, 1) } } + findAll(matcher){ - return this.items.filter(matcher) + const matches = [] + for(let item of this.items){ + if(matcher(item)){ + matches.push(item) + } + } + return matches + + // funktionaler ansatz + // return this.items.filter(matcher) } -} \ No newline at end of file +} diff --git a/src/modules/app.js b/src/modules/app.js new file mode 100644 index 0000000..a31d8ca --- /dev/null +++ b/src/modules/app.js @@ -0,0 +1,14 @@ +// 'use strict' // optional, weil im Browser via + + + + + diff --git a/src/modules/logger.js b/src/modules/logger.js deleted file mode 100644 index ef28d50..0000000 --- a/src/modules/logger.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict' -export default class { - constructor(writer){ - this.writer = writer - } - warn(msg){ - this.writer('WARNING: ' + msg) - } - log(msg){ - this.writer('LOG: ' + msg) - } -} - -export const PI=3.141592653 \ No newline at end of file diff --git a/src/modules/logger.module.js b/src/modules/logger.module.js new file mode 100644 index 0000000..3fcdf23 --- /dev/null +++ b/src/modules/logger.module.js @@ -0,0 +1,20 @@ +// 'use strict' // optional, weil im Browser via