From e5ae0c75cf87d4400f1c9b8f76e733b66ff4b65a Mon Sep 17 00:00:00 2001 From: Niels Kooiman Date: Sun, 18 Feb 2018 00:18:16 +0100 Subject: [PATCH] added example files --- dist/adapters/music/VinylCatalog.js | 35 +++++++++++++ dist/domain/factories/IPortProvider.js | 2 + dist/domain/models/Track.js | 12 +++++ dist/domain/ports/IMusicRepository.js | 2 + dist/domain/services/MusicCatalogService.js | 24 +++++++++ dist/view/MusicComponent.js | 20 ++++++++ dist/view/MusicComponent.spec.js | 9 ++++ karma.conf.js | 20 ++++++++ package.json | 33 ++++++++++++ src/adapters/music/VinylCatalog.ts | 39 ++++++++++++++ src/domain/factories/IPortProvider.ts | 4 ++ src/domain/models/Track.ts | 15 ++++++ src/domain/ports/IMusicRepository.ts | 10 ++++ src/domain/services/MusicCatalogService.ts | 27 ++++++++++ src/view/MusicComponent.spec.ts | 10 ++++ src/view/MusicComponent.ts | 23 +++++++++ tsconfig.json | 57 +++++++++++++++++++++ 17 files changed, 342 insertions(+) create mode 100644 dist/adapters/music/VinylCatalog.js create mode 100644 dist/domain/factories/IPortProvider.js create mode 100644 dist/domain/models/Track.js create mode 100644 dist/domain/ports/IMusicRepository.js create mode 100644 dist/domain/services/MusicCatalogService.js create mode 100644 dist/view/MusicComponent.js create mode 100644 dist/view/MusicComponent.spec.js create mode 100644 karma.conf.js create mode 100644 package.json create mode 100644 src/adapters/music/VinylCatalog.ts create mode 100644 src/domain/factories/IPortProvider.ts create mode 100644 src/domain/models/Track.ts create mode 100644 src/domain/ports/IMusicRepository.ts create mode 100644 src/domain/services/MusicCatalogService.ts create mode 100644 src/view/MusicComponent.spec.ts create mode 100644 src/view/MusicComponent.ts create mode 100644 tsconfig.json diff --git a/dist/adapters/music/VinylCatalog.js b/dist/adapters/music/VinylCatalog.js new file mode 100644 index 0000000..67634c6 --- /dev/null +++ b/dist/adapters/music/VinylCatalog.js @@ -0,0 +1,35 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var Track_1 = require("../../domain/models/Track"); +var VinylCatalog = /** @class */ (function () { + function VinylCatalog() { + this.vinylList = new Array(new Track_1.Track(1, "DNA.", "Kendrick Lamar", 340), new Track_1.Track(2, "Come Down", "Anderson Paak.", 430), new Track_1.Track(3, "DNA.", "Kendrick Lamar", 340), new Track_1.Track(4, "DNA.", "Kendrick Lamar", 340), new Track_1.Track(5, "DNA.", "Kendrick Lamar", 340)); + } + VinylCatalog.prototype.get = function () { + return this.vinylList; + }; + VinylCatalog.prototype.getById = function (id) { + return this.vinylList.filter(function (track) { return track.Id == id; }).pop(); + }; + VinylCatalog.prototype.add = function (track) { + return this.vinylList.push(track); + }; + VinylCatalog.prototype.edit = function (id, track) { + var existingTrack = this.getById(id); + existingTrack.Artist = track.Artist; + existingTrack.Title = track.Title; + existingTrack.Duration = track.Duration; + return existingTrack; + }; + VinylCatalog.prototype.delete = function (id) { + var track = this.getById(id); + if (track) { + var targetIndex = this.vinylList.indexOf(track); + if (targetIndex < -1) + return null; + return this.vinylList.splice(targetIndex, 1)[0]; + } + }; + return VinylCatalog; +}()); +exports.VinylCatalog = VinylCatalog; diff --git a/dist/domain/factories/IPortProvider.js b/dist/domain/factories/IPortProvider.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/dist/domain/factories/IPortProvider.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/dist/domain/models/Track.js b/dist/domain/models/Track.js new file mode 100644 index 0000000..a8ec838 --- /dev/null +++ b/dist/domain/models/Track.js @@ -0,0 +1,12 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var Track = /** @class */ (function () { + function Track(id, title, artist, duration) { + this.Id = id; + this.Title = title; + this.Artist = artist; + this.Duration = duration; + } + return Track; +}()); +exports.Track = Track; diff --git a/dist/domain/ports/IMusicRepository.js b/dist/domain/ports/IMusicRepository.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/dist/domain/ports/IMusicRepository.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/dist/domain/services/MusicCatalogService.js b/dist/domain/services/MusicCatalogService.js new file mode 100644 index 0000000..c44feb0 --- /dev/null +++ b/dist/domain/services/MusicCatalogService.js @@ -0,0 +1,24 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var MusicCatalogService = /** @class */ (function () { + function MusicCatalogService(repository) { + this.repository = repository; + } + MusicCatalogService.prototype.get = function () { + return this.repository.get(); + }; + MusicCatalogService.prototype.getById = function (id) { + return this.repository.getById(id); + }; + MusicCatalogService.prototype.add = function (track) { + return this.repository.add(track); + }; + MusicCatalogService.prototype.edit = function (id, track) { + return this.repository.edit(id, track); + }; + MusicCatalogService.prototype.delete = function (id) { + return this.repository.delete(id); + }; + return MusicCatalogService; +}()); +exports.MusicCatalogService = MusicCatalogService; diff --git a/dist/view/MusicComponent.js b/dist/view/MusicComponent.js new file mode 100644 index 0000000..960db99 --- /dev/null +++ b/dist/view/MusicComponent.js @@ -0,0 +1,20 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var MusicCatalogService_1 = require("../domain/services/MusicCatalogService"); +var VinylCatalog_1 = require("../adapters/music/VinylCatalog"); +var MusicComponent = /** @class */ (function () { + function MusicComponent() { + var container = { + IMusicRepository: function () { return new VinylCatalog_1.VinylCatalog(); } + }; + var inject = function (name) { + if (container[name]) { + return container[name]; + } + throw new Error("Failed to resolve " + name); + }; + var musicCatalogService = new MusicCatalogService_1.MusicCatalogService(inject("IMusicRepository")); + } + return MusicComponent; +}()); +exports.MusicComponent = MusicComponent; diff --git a/dist/view/MusicComponent.spec.js b/dist/view/MusicComponent.spec.js new file mode 100644 index 0000000..7f47089 --- /dev/null +++ b/dist/view/MusicComponent.spec.js @@ -0,0 +1,9 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var MusicComponent_1 = require("./MusicComponent"); +describe("MusicComponent", function () { + it('should create a new MusicComponent', function (done) { + var musicComponent = new MusicComponent_1.MusicComponent(); + done(); + }); +}); diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..d248d16 --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,20 @@ + +module.exports = function(config) { + config.set({ + frameworks: ["jasmine", "karma-typescript"], + files: [ + { pattern: "src/**/*.spec.ts" } + ], + karmaTypescriptConfig: { + compilerOptions: { + module: "commonjs" + }, + tsconfig: "./tsconfig.json", + }, + preprocessors: { + "**/*.ts": ["karma-typescript"] + }, + reporters: ["progress", "karma-typescript"], + browsers: ["Chrome"] + }); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..2e20839 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "typescript-starter", + "version": "1.0.0", + "description": "Starter project for a typescript app", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "tsc --listEmittedFiles", + "_install": "tsc --listEmittedFiles", + "test": "karma start karma.conf.js -sm=false", + "test-build": "karma start karma.conf.js -sm=false --watch=false --single-run=true --reporters=junit,progress --browsers=Chrome" + }, + "repository": { + "type": "git", + "url": "https://niels.kooiman@solidt.eu/git/niels.kooiman/typescript-starter.git" + }, + "author": "Niels Kooiman ", + "license": "UNLICENCED", + "devDependencies": { + "@types/jasmine": "^2.8.6", + "diff": "^3.4.0", + "handlebars": "^4.0.11", + "jasmine-core": "^2.99.1", + "karma": "^1.7.1", + "karma-chrome-launcher": "^2.2.0", + "karma-cli": "^1.0.1", + "karma-jasmine": "^1.1.1", + "karma-typescript": "^3.0.12", + "socket.io": "^2.0.4", + "typescript": "^2.7.2" + }, + "dependencies": {} +} diff --git a/src/adapters/music/VinylCatalog.ts b/src/adapters/music/VinylCatalog.ts new file mode 100644 index 0000000..18130b5 --- /dev/null +++ b/src/adapters/music/VinylCatalog.ts @@ -0,0 +1,39 @@ +import { Track } from "../../domain/models/Track"; +import { IMusicRepository } from "../../domain/ports/IMusicRepository"; + +export class VinylCatalog implements IMusicRepository { + + private vinylList: Track[] = new Array( + new Track(1, "DNA.", "Kendrick Lamar", 340), + new Track(2, "Come Down", "Anderson Paak.", 430), + new Track(3, "DNA.", "Kendrick Lamar", 340), + new Track(4, "DNA.", "Kendrick Lamar", 340), + new Track(5, "DNA.", "Kendrick Lamar", 340) + ); + + get(): Track[] { + return this.vinylList; + } + getById(id: number): Track { + return this.vinylList.filter(track => track.Id == id).pop(); + } + add(track: Track): number { + return this.vinylList.push(track); + } + edit(id: number, track: Track): Track { + var existingTrack = this.getById(id); + existingTrack.Artist = track.Artist; + existingTrack.Title = track.Title; + existingTrack.Duration = track.Duration; + return existingTrack; + + } + delete(id: number): Track { + var track = this.getById(id); + if (track) { + var targetIndex = this.vinylList.indexOf(track); + if (targetIndex < -1) return null; + return this.vinylList.splice(targetIndex, 1)[0]; + } + } +} diff --git a/src/domain/factories/IPortProvider.ts b/src/domain/factories/IPortProvider.ts new file mode 100644 index 0000000..033539c --- /dev/null +++ b/src/domain/factories/IPortProvider.ts @@ -0,0 +1,4 @@ + +export interface IPortProvider { + +} diff --git a/src/domain/models/Track.ts b/src/domain/models/Track.ts new file mode 100644 index 0000000..01cac9d --- /dev/null +++ b/src/domain/models/Track.ts @@ -0,0 +1,15 @@ + +export class Track{ + + constructor(id: number, title: string, artist: string, duration:number){ + this.Id= id; + this.Title= title; + this.Artist= artist; + this.Duration= duration; + } + + public Id : number; + public Title: string; + public Artist: string; + public Duration: number; +} diff --git a/src/domain/ports/IMusicRepository.ts b/src/domain/ports/IMusicRepository.ts new file mode 100644 index 0000000..2cb9bba --- /dev/null +++ b/src/domain/ports/IMusicRepository.ts @@ -0,0 +1,10 @@ +import { Track } from "../models/Track"; + + +export interface IMusicRepository { + get() : Track[]; + getById(id: number) : Track; + add(track: Track) : number; + edit(id: number, track: Track) : Track; + delete(id: number) : Track; +} diff --git a/src/domain/services/MusicCatalogService.ts b/src/domain/services/MusicCatalogService.ts new file mode 100644 index 0000000..2b80dae --- /dev/null +++ b/src/domain/services/MusicCatalogService.ts @@ -0,0 +1,27 @@ +import { IMusicRepository } from "../../domain/ports/IMusicRepository"; +import { Track } from "../../domain/models/Track"; + + +export class MusicCatalogService { + private repository: IMusicRepository; + + constructor(repository:IMusicRepository){ + this.repository= repository; + } + + get(): Track[] { + return this.repository.get(); + } + getById(id: number): Track { + return this.repository.getById(id); + } + add(track: Track): number { + return this.repository.add(track); + } + edit(id: number, track: Track): Track { + return this.repository.edit(id, track); + } + delete(id: number): Track { + return this.repository.delete(id); + } +} diff --git a/src/view/MusicComponent.spec.ts b/src/view/MusicComponent.spec.ts new file mode 100644 index 0000000..a88a0a9 --- /dev/null +++ b/src/view/MusicComponent.spec.ts @@ -0,0 +1,10 @@ +import { MusicComponent } from "./MusicComponent"; + +describe("MusicComponent", () => { + + it('should create a new MusicComponent', (done) => { + let musicComponent = new MusicComponent(); + done(); + }); + +}); diff --git a/src/view/MusicComponent.ts b/src/view/MusicComponent.ts new file mode 100644 index 0000000..c4890bd --- /dev/null +++ b/src/view/MusicComponent.ts @@ -0,0 +1,23 @@ + +import { MusicCatalogService } from "../domain/services/MusicCatalogService" +import { IMusicRepository } from "../domain/ports/IMusicRepository"; + +import { VinylCatalog } from "../adapters/music/VinylCatalog" + +export class MusicComponent { + constructor() { + + let container = { + IMusicRepository: () => new VinylCatalog() + }; + + let inject = (name : string) : any => { + if (container[name]) { + return container[name]; + } + throw new Error(`Failed to resolve ${name}`) + }; + let musicCatalogService = new MusicCatalogService(inject("IMusicRepository") as IMusicRepository); + + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..fd49916 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,57 @@ +{ + "compilerOptions": { + /* Basic Options */ + "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' 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. */ + // "checkJs": true, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ + // "declaration": true, /* Generates corresponding '.d.ts' file. */ + // "sourceMap": true, /* Generates corresponding '.map' file. */ + // "outFile": "./", /* Concatenate and emit output to single file. */ + "outDir": "./dist", /* Redirect output structure to the directory. */ + // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "removeComments": true, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + + /* Strict Type-Checking Options */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + + /* Additional Checks */ + // "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + + /* Module Resolution Options */ + // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + + /* Source Map Options */ + // "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + + /* Experimental Options */ + // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + } +}