From 479e8243afc42130dd4edbb8e59c6b97e7cd6511 Mon Sep 17 00:00:00 2001 From: Anthony Fung Date: Thu, 25 Oct 2018 16:26:54 +0100 Subject: [PATCH] Add source maps; add trap location generator --- karma.conf.js | 1 + package-lock.json | 21 ++++++++ package.json | 1 + src/services/trap-location-generator.js | 25 +++++++++ test/services/trap-location-generator.spec.js | 52 +++++++++++++++++++ webpack.config.js | 11 +++- 6 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/services/trap-location-generator.js create mode 100644 test/services/trap-location-generator.spec.js diff --git a/karma.conf.js b/karma.conf.js index e9e040b..b156943 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -21,6 +21,7 @@ module.exports = function(config) { webpack: { module: webpackConfig.module, + plugins: webpackConfig.plugins, resolve: webpackConfig.resolve }, diff --git a/package-lock.json b/package-lock.json index c8a635c..4a544f2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6933,6 +6933,27 @@ "integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=", "dev": true }, + "source-map-loader": { + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/source-map-loader/-/source-map-loader-0.2.4.tgz", + "integrity": "sha512-OU6UJUty+i2JDpTItnizPrlpOIBLmQbWMuBg9q5bVtnHACqw1tn9nNwqJLbv0/00JjnJb/Ee5g5WS5vrRv7zIQ==", + "dev": true, + "requires": { + "async": "^2.5.0", + "loader-utils": "^1.1.0" + }, + "dependencies": { + "async": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.1.tgz", + "integrity": "sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ==", + "dev": true, + "requires": { + "lodash": "^4.17.10" + } + } + } + }, "source-map-resolve": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/source-map-resolve/-/source-map-resolve-0.5.2.tgz", diff --git a/package.json b/package.json index ad577c6..54b37d1 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "karma-mocha": "^1.3.0", "karma-webpack": "^3.0.5", "mocha": "^5.2.0", + "source-map-loader": "^0.2.4", "webpack": "^4.22.0", "webpack-cli": "^3.1.2", "webpack-dev-server": "^3.1.10" diff --git a/src/services/trap-location-generator.js b/src/services/trap-location-generator.js new file mode 100644 index 0000000..d39eb27 --- /dev/null +++ b/src/services/trap-location-generator.js @@ -0,0 +1,25 @@ +class LocationGenerator { + getRandomInt(max) { + return Math.floor(Math.random() * Math.floor(max)); + } + + generateLocations(width, height, trapCount) { + const traps = {}; + + for (let n = 0; n < trapCount; n++) { + const x = this.getRandomInt(width); + const y = this.getRandomInt(height); + + if (traps[x]) { + traps[x].push(y); + } + else { + traps[x] = [y]; + } + } + + return traps; + } +} + +export default LocationGenerator; diff --git a/test/services/trap-location-generator.spec.js b/test/services/trap-location-generator.spec.js new file mode 100644 index 0000000..7d8fce9 --- /dev/null +++ b/test/services/trap-location-generator.spec.js @@ -0,0 +1,52 @@ +/* global describe, it */ + +import { expect } from 'chai'; +import Generator from '../../src/services/trap-location-generator'; + +describe('trap location generator', function() { + it('returns new trap locations', function() { + + // Arrange + + const count = 3; + const height = 3; + const width = 3; + + // Act + + const generator = new Generator(); + const locations = generator.generateLocations(width, height, count); + + // Assert + + const keys = Object.keys(locations); + expect(keys.length).to.be.greaterThan(0); + + keys.forEach(xValue => { + expect(Number(xValue), 'x co-ordinate should be at least 0').to.be.at.least(0); + + locations[xValue].forEach(yValue => { + expect(yValue, 'y co-ordinate should be at least 0').to.be.at.least(0); + }); + }); + }); + + it('returns different locations each time', function() { + + // Arrange + + const count = 3; + const height = 3; + const width = 3; + + // Act + + const generator = new Generator(); + const locations1 = generator.generateLocations(width, height, count); + const locations2 = generator.generateLocations(width, height, count); + + // Assert + + expect(locations1).not.to.deep.equal(locations2); + }); +}); diff --git a/webpack.config.js b/webpack.config.js index 4178ab9..0498ca2 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,4 +1,6 @@ -/* global module, __dirname */ +/* global __dirname, module, require */ + +const webpack = require('webpack'); module.exports = { devServer: { @@ -7,6 +9,13 @@ module.exports = { }, mode: 'development', + + plugins: [ + new webpack.SourceMapDevToolPlugin({ + filename: null, // inline sourcemap + test: /\.js($|\?)/i // case-insensitive match for js files + }) + ], entry: './src/index.jsx',