@superhero/core 1.8.5 → 4.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -739
- package/index.js +654 -54
- package/index.test.js +94 -0
- package/package.json +17 -31
- package/worker.js +12 -0
- package/LICENCE +0 -20
- package/bootstrap/config.js +0 -7
- package/bootstrap/index.js +0 -19
- package/bootstrap/locator.js +0 -16
- package/cli/config.js +0 -7
- package/cli/index.js +0 -49
- package/cli/locator.js +0 -17
- package/composer/bootstrap/error/schema-not-resolvable.js +0 -10
- package/composer/bootstrap/index.js +0 -62
- package/composer/bootstrap/locator.js +0 -20
- package/composer/config.js +0 -49
- package/composer/error/filter-is-not-honering-contract.js +0 -13
- package/composer/error/invalid-attribute.js +0 -13
- package/composer/error/invalid-collection.js +0 -13
- package/composer/error/invalid-schema.js +0 -13
- package/composer/error/schema-not-found.js +0 -13
- package/composer/error/validator-is-not-honering-contract.js +0 -13
- package/composer/error/validator-not-found.js +0 -13
- package/composer/filter/boolean/index.js +0 -44
- package/composer/filter/boolean/locator.js +0 -11
- package/composer/filter/decimal/index.js +0 -38
- package/composer/filter/decimal/locator.js +0 -11
- package/composer/filter/index.js +0 -10
- package/composer/filter/integer/index.js +0 -38
- package/composer/filter/integer/locator.js +0 -11
- package/composer/filter/json/index.js +0 -23
- package/composer/filter/json/locator.js +0 -11
- package/composer/filter/schema/error/missing-schema-definition.js +0 -13
- package/composer/filter/schema/index.js +0 -49
- package/composer/filter/schema/locator.js +0 -17
- package/composer/filter/string/index.js +0 -50
- package/composer/filter/string/locator.js +0 -11
- package/composer/filter/timestamp/index.js +0 -45
- package/composer/filter/timestamp/locator.js +0 -11
- package/composer/index.js +0 -180
- package/composer/locator.js +0 -17
- package/composer/validator/boolean/error/invalid.js +0 -13
- package/composer/validator/boolean/index.js +0 -17
- package/composer/validator/boolean/locator.js +0 -11
- package/composer/validator/decimal/error/invalid.js +0 -13
- package/composer/validator/decimal/index.js +0 -59
- package/composer/validator/decimal/locator.js +0 -11
- package/composer/validator/index.js +0 -10
- package/composer/validator/integer/error/invalid.js +0 -13
- package/composer/validator/integer/index.js +0 -65
- package/composer/validator/integer/locator.js +0 -11
- package/composer/validator/json/error/invalid.js +0 -13
- package/composer/validator/json/index.js +0 -23
- package/composer/validator/json/locator.js +0 -11
- package/composer/validator/schema/error/invalid.js +0 -13
- package/composer/validator/schema/index.js +0 -13
- package/composer/validator/schema/locator.js +0 -11
- package/composer/validator/string/error/invalid.js +0 -13
- package/composer/validator/string/index.js +0 -59
- package/composer/validator/string/locator.js +0 -11
- package/composer/validator/timestamp/error/invalid.js +0 -13
- package/composer/validator/timestamp/index.js +0 -54
- package/composer/validator/timestamp/locator.js +0 -11
- package/configuration/config.js +0 -7
- package/configuration/index.js +0 -23
- package/configuration/locator.js +0 -22
- package/console/config.js +0 -15
- package/console/index.js +0 -23
- package/console/locator.js +0 -21
- package/console/observer/error/config.js +0 -7
- package/console/observer/error/index.js +0 -14
- package/console/observer/error/locator.js +0 -17
- package/console/observer/info/config.js +0 -7
- package/console/observer/info/index.js +0 -14
- package/console/observer/info/locator.js +0 -17
- package/console/observer/warning/config.js +0 -7
- package/console/observer/warning/index.js +0 -14
- package/console/observer/warning/locator.js +0 -17
- package/core/config.js +0 -21
- package/core/error.js +0 -10
- package/deepcopy/config.js +0 -7
- package/deepcopy/error/failed-to-fast-copy.js +0 -10
- package/deepcopy/index.js +0 -18
- package/deepcopy/locator.js +0 -11
- package/deepfind/config.js +0 -7
- package/deepfind/index.js +0 -11
- package/deepfind/locator.js +0 -11
- package/deepfreeze/config.js +0 -7
- package/deepfreeze/index.js +0 -20
- package/deepfreeze/locator.js +0 -11
- package/deepmerge/config.js +0 -7
- package/deepmerge/index.js +0 -42
- package/deepmerge/locator.js +0 -11
- package/eventbus/bootstrap/error/observer-contract-not-honered.js +0 -10
- package/eventbus/bootstrap/index.js +0 -33
- package/eventbus/bootstrap/locator.js +0 -20
- package/eventbus/config.js +0 -17
- package/eventbus/index.js +0 -21
- package/eventbus/locator.js +0 -25
- package/eventbus/observer.js +0 -9
- package/factory.js +0 -55
- package/http/request/config.js +0 -7
- package/http/request/locator.js +0 -21
- package/http/server/config.js +0 -23
- package/http/server/dispatcher/chain/error/dispatcher-chain-ended.js +0 -10
- package/http/server/dispatcher/chain/index.js +0 -41
- package/http/server/dispatcher/chain/locator.js +0 -20
- package/http/server/dispatcher/collection/builder/error/dispatcher-can-not-be-resolved.js +0 -10
- package/http/server/dispatcher/collection/builder/error/not-honering-dispatcher-contract.js +0 -10
- package/http/server/dispatcher/collection/builder/index.js +0 -56
- package/http/server/dispatcher/collection/builder/locator.js +0 -20
- package/http/server/dispatcher/error/bad-gateway.js +0 -12
- package/http/server/dispatcher/error/bad-request.js +0 -12
- package/http/server/dispatcher/error/conflict.js +0 -12
- package/http/server/dispatcher/error/forbidden.js +0 -12
- package/http/server/dispatcher/error/gateway-timeout.js +0 -12
- package/http/server/dispatcher/error/index.js +0 -10
- package/http/server/dispatcher/error/method-not-allowed.js +0 -12
- package/http/server/dispatcher/error/not-implemented.js +0 -12
- package/http/server/dispatcher/error/page-not-found.js +0 -12
- package/http/server/dispatcher/error/request-timeout.js +0 -12
- package/http/server/dispatcher/error/server-error.js +0 -12
- package/http/server/dispatcher/error/service-unavailable.js +0 -12
- package/http/server/dispatcher/error/unauthorized.js +0 -12
- package/http/server/dispatcher/index.js +0 -26
- package/http/server/dispatcher/rest.js +0 -75
- package/http/server/error/no-endpoint-defined-in-route.js +0 -10
- package/http/server/error/view-contract-not-honered.js +0 -10
- package/http/server/index.js +0 -195
- package/http/server/locator.js +0 -34
- package/http/server/request/builder/index.js +0 -53
- package/http/server/request/builder/locator.js +0 -20
- package/http/server/route/builder/error/dto-invalid-reference.js +0 -10
- package/http/server/route/builder/error/routes-invalid-type.js +0 -10
- package/http/server/route/builder/index.js +0 -88
- package/http/server/route/builder/locator.js +0 -20
- package/http/server/session/builder/index.js +0 -34
- package/http/server/session/builder/locator.js +0 -11
- package/http/server/view/index.js +0 -12
- package/http/server/view/json/index.js +0 -17
- package/http/server/view/json/locator.js +0 -11
- package/http/server/view/locator.js +0 -11
- package/http/server/view/stream/index.js +0 -10
- package/http/server/view/stream/locator.js +0 -11
- package/http/server/view/text/index.js +0 -15
- package/http/server/view/text/locator.js +0 -11
- package/locator/constituent.js +0 -26
- package/locator/error/locator-not-implemented.js +0 -10
- package/locator/error/service-undefined.js +0 -10
- package/locator/index.js +0 -29
- package/path/config.js +0 -7
- package/path/index.js +0 -63
- package/path/locator.js +0 -11
- package/process/bootstrap/index.js +0 -31
- package/process/bootstrap/locator.js +0 -17
- package/process/config.js +0 -12
- package/process/index.js +0 -9
- package/process/locator.js +0 -11
- package/test/api/config.js +0 -43
- package/test/api/endpoint/append-calculation.js +0 -41
- package/test/api/endpoint/create-calculation.js +0 -18
- package/test/api/middleware/authentication.js +0 -27
- package/test/calculator/calculation.js +0 -29
- package/test/calculator/config.js +0 -14
- package/test/calculator/error/calculation-could-not-be-found.js +0 -13
- package/test/calculator/error/invalid-calculation-type.js +0 -13
- package/test/calculator/index.js +0 -67
- package/test/calculator/locator.js +0 -20
- package/test/init.js +0 -2
- package/test/logger/config.js +0 -15
- package/test/logger/index.js +0 -17
- package/test/logger/locator.js +0 -20
- package/test/mocha.opts +0 -4
- package/test/test.calculation.js +0 -58
- package/test/test.logger.js +0 -35
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
class HttpViewText
|
|
2
|
-
{
|
|
3
|
-
write(output, viewModel)
|
|
4
|
-
{
|
|
5
|
-
const body = `${viewModel.body}`
|
|
6
|
-
|
|
7
|
-
viewModel.headers['Content-Type'] = 'text/plain'
|
|
8
|
-
viewModel.headers['Content-Length'] = Buffer.byteLength(body)
|
|
9
|
-
|
|
10
|
-
output.writeHead(viewModel.status || 200, viewModel.headers)
|
|
11
|
-
output.end(body)
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
module.exports = HttpViewText
|
package/locator/constituent.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const LocatorNotImplementedError = require('./error/locator-not-implemented')
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* For classes that represent a locator constituent of a composite pattern.
|
|
5
|
-
*
|
|
6
|
-
* @abstract
|
|
7
|
-
*/
|
|
8
|
-
class LocatorConstituent
|
|
9
|
-
{
|
|
10
|
-
constructor(locator)
|
|
11
|
-
{
|
|
12
|
-
this.locator = locator
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* A factory method for a service
|
|
17
|
-
* @returns {*} An instenace of the service that is being located
|
|
18
|
-
* @abstract
|
|
19
|
-
*/
|
|
20
|
-
locate()
|
|
21
|
-
{
|
|
22
|
-
throw new LocatorNotImplementedError('the "locate" method was not overwritten')
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
module.exports = LocatorConstituent
|
package/locator/index.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
const ServiceUndefinedError = require('./error/service-undefined')
|
|
2
|
-
|
|
3
|
-
class Locator
|
|
4
|
-
{
|
|
5
|
-
constructor()
|
|
6
|
-
{
|
|
7
|
-
this.services = {}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
set(name, service)
|
|
11
|
-
{
|
|
12
|
-
this.services[name] = service
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
has(name)
|
|
16
|
-
{
|
|
17
|
-
return name in this.services
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
locate(service)
|
|
21
|
-
{
|
|
22
|
-
if(service in this.services)
|
|
23
|
-
return this.services[service]
|
|
24
|
-
|
|
25
|
-
throw new ServiceUndefinedError(`"${service}" can not be located`)
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
module.exports = Locator
|
package/path/config.js
DELETED
package/path/index.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
const path = require('path')
|
|
2
|
-
|
|
3
|
-
class Path
|
|
4
|
-
{
|
|
5
|
-
constructor()
|
|
6
|
-
{
|
|
7
|
-
const
|
|
8
|
-
filename = require.main.filename,
|
|
9
|
-
dirname = this.dirname(filename)
|
|
10
|
-
|
|
11
|
-
this.main = { filename, dirname }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @see require.resolve
|
|
16
|
-
*/
|
|
17
|
-
isResolvable(filename)
|
|
18
|
-
{
|
|
19
|
-
try
|
|
20
|
-
{
|
|
21
|
-
require.resolve(filename)
|
|
22
|
-
return true
|
|
23
|
-
}
|
|
24
|
-
catch (error)
|
|
25
|
-
{
|
|
26
|
-
return false
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @see path.dirname
|
|
32
|
-
*/
|
|
33
|
-
dirname(filename)
|
|
34
|
-
{
|
|
35
|
-
return path.dirname(filename)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @see path.normalize
|
|
40
|
-
*/
|
|
41
|
-
normalize(filename)
|
|
42
|
-
{
|
|
43
|
-
return path.normalize(filename)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @see path.extname
|
|
48
|
-
*/
|
|
49
|
-
extension(filename)
|
|
50
|
-
{
|
|
51
|
-
return path.extname(filename)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* @see path.isAbsolute
|
|
56
|
-
*/
|
|
57
|
-
isAbsolute(filename)
|
|
58
|
-
{
|
|
59
|
-
return path.isAbsolute(filename)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
module.exports = Path
|
package/path/locator.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
class EventBusBootstrap
|
|
2
|
-
{
|
|
3
|
-
constructor(eventbus)
|
|
4
|
-
{
|
|
5
|
-
this.eventbus = eventbus
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
bootstrap()
|
|
9
|
-
{
|
|
10
|
-
process.on('unhandledRejection', this.onError.bind(this))
|
|
11
|
-
process.on('uncaughtException', this.onError.bind(this))
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
onError(error, rejectedPromise)
|
|
15
|
-
{
|
|
16
|
-
if(rejectedPromise && rejectedPromise.domain)
|
|
17
|
-
{
|
|
18
|
-
rejectedPromise.domain.emit('error', error)
|
|
19
|
-
}
|
|
20
|
-
else if(process.domain)
|
|
21
|
-
{
|
|
22
|
-
process.domain.emit('error', error)
|
|
23
|
-
}
|
|
24
|
-
else
|
|
25
|
-
{
|
|
26
|
-
this.eventbus.emit('core.error', error)
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
module.exports = EventBusBootstrap
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
const ProcessBootstrap = require('.')
|
|
2
|
-
|
|
3
|
-
class ProcessBootstrapLocator
|
|
4
|
-
{
|
|
5
|
-
constructor(locator)
|
|
6
|
-
{
|
|
7
|
-
this.locator = locator
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
locate()
|
|
11
|
-
{
|
|
12
|
-
const eventbus = this.locator.locate('eventbus')
|
|
13
|
-
return new ProcessBootstrap(eventbus)
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
module.exports = ProcessBootstrapLocator
|
package/process/config.js
DELETED
package/process/index.js
DELETED
package/process/locator.js
DELETED
package/test/api/config.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
module.exports =
|
|
2
|
-
{
|
|
3
|
-
http:
|
|
4
|
-
{
|
|
5
|
-
server:
|
|
6
|
-
{
|
|
7
|
-
routes:
|
|
8
|
-
{
|
|
9
|
-
'create-calculation':
|
|
10
|
-
{
|
|
11
|
-
url : '/calculations',
|
|
12
|
-
method : 'post',
|
|
13
|
-
endpoint: 'api/endpoint/create-calculation',
|
|
14
|
-
view : 'http/server/view/json'
|
|
15
|
-
},
|
|
16
|
-
'authentication':
|
|
17
|
-
{
|
|
18
|
-
middleware :
|
|
19
|
-
[
|
|
20
|
-
'api/middleware/authentication'
|
|
21
|
-
]
|
|
22
|
-
},
|
|
23
|
-
'append-calculation':
|
|
24
|
-
{
|
|
25
|
-
url : '/calculations/.+',
|
|
26
|
-
method : 'put',
|
|
27
|
-
endpoint: 'api/endpoint/append-calculation',
|
|
28
|
-
view : 'http/server/view/json',
|
|
29
|
-
dto :
|
|
30
|
-
{
|
|
31
|
-
'id' : { 'url' : 2 },
|
|
32
|
-
'type' : { 'body' : 'type' },
|
|
33
|
-
'value' : { 'body' : 'value' }
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
authentication:
|
|
40
|
-
{
|
|
41
|
-
apikey : 'ABC123456789'
|
|
42
|
-
}
|
|
43
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const
|
|
2
|
-
Dispatcher = require('../../../http/server/dispatcher'),
|
|
3
|
-
PageNotFoundError = require('../../../http/server/dispatcher/error/page-not-found'),
|
|
4
|
-
BadRequestError = require('../../../http/server/dispatcher/error/bad-request')
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @extends {@superhero/core/http/server/dispatcher}
|
|
8
|
-
*/
|
|
9
|
-
class AppendCalculationEndpoint extends Dispatcher
|
|
10
|
-
{
|
|
11
|
-
dispatch()
|
|
12
|
-
{
|
|
13
|
-
const
|
|
14
|
-
calculator = this.locator.locate('calculator'),
|
|
15
|
-
composer = this.locator.locate('composer'),
|
|
16
|
-
calculation = composer.compose('calculation', this.route.dto),
|
|
17
|
-
result = calculator.appendToCalculation(calculation)
|
|
18
|
-
|
|
19
|
-
this.view.body.result = result
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
onError(error)
|
|
23
|
-
{
|
|
24
|
-
switch(error)
|
|
25
|
-
{
|
|
26
|
-
case 'E_CALCULATION_COULD_NOT_BE_FOUND':
|
|
27
|
-
throw new PageNotFoundError('Calculation could not be found')
|
|
28
|
-
|
|
29
|
-
case 'E_INVALID_CALCULATION_TYPE':
|
|
30
|
-
throw new BadRequestError(`Unrecognized type: "${this.route.dto.type}"`)
|
|
31
|
-
|
|
32
|
-
case 'E_COMPOSER_INVALID_ATTRIBUTE':
|
|
33
|
-
throw new BadRequestError(error.message)
|
|
34
|
-
|
|
35
|
-
default:
|
|
36
|
-
throw error
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
module.exports = AppendCalculationEndpoint
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const Dispatcher = require('../../../http/server/dispatcher')
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @extends {@superhero/core/http/server/dispatcher}
|
|
5
|
-
*/
|
|
6
|
-
class CreateCalculationEndpoint extends Dispatcher
|
|
7
|
-
{
|
|
8
|
-
dispatch()
|
|
9
|
-
{
|
|
10
|
-
const
|
|
11
|
-
calculator = this.locator.locate('calculator'),
|
|
12
|
-
calculationId = calculator.createCalculation()
|
|
13
|
-
|
|
14
|
-
this.view.body.id = calculationId
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
module.exports = CreateCalculationEndpoint
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
const
|
|
2
|
-
Dispatcher = require('../../../http/server/dispatcher'),
|
|
3
|
-
Unauthorized = require('../../../http/server/dispatcher/error/unauthorized')
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @extends {@superhero/core/http/server/dispatcher}
|
|
7
|
-
*/
|
|
8
|
-
class AuthenticationMiddleware extends Dispatcher
|
|
9
|
-
{
|
|
10
|
-
async dispatch(next)
|
|
11
|
-
{
|
|
12
|
-
const
|
|
13
|
-
configuration = this.locator.locate('configuration'),
|
|
14
|
-
apikey = this.request.headers['api-key']
|
|
15
|
-
|
|
16
|
-
if(apikey === configuration.find('authentication.apikey'))
|
|
17
|
-
{
|
|
18
|
-
await next()
|
|
19
|
-
}
|
|
20
|
-
else
|
|
21
|
-
{
|
|
22
|
-
throw new Unauthorized('You are not authorized to access the requested resource')
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
module.exports = AuthenticationMiddleware
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {Object} CalculatorCalculation
|
|
3
|
-
* @property {number} id
|
|
4
|
-
* @property {string} type
|
|
5
|
-
* @property {number} value
|
|
6
|
-
*/
|
|
7
|
-
const dto =
|
|
8
|
-
{
|
|
9
|
-
'id':
|
|
10
|
-
{
|
|
11
|
-
'type' : 'integer',
|
|
12
|
-
'unsigned': true
|
|
13
|
-
},
|
|
14
|
-
'type':
|
|
15
|
-
{
|
|
16
|
-
'type': 'string',
|
|
17
|
-
'enum':
|
|
18
|
-
[
|
|
19
|
-
'addition',
|
|
20
|
-
'subtraction'
|
|
21
|
-
]
|
|
22
|
-
},
|
|
23
|
-
'value':
|
|
24
|
-
{
|
|
25
|
-
'type': 'decimal'
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
module.exports = dto
|
package/test/calculator/index.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
const
|
|
2
|
-
CalculationCouldNotBeFoundError = require('./error/calculation-could-not-be-found'),
|
|
3
|
-
InvalidCalculationTypeError = require('./error/invalid-calculation-type')
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Calculator service, manages calculations
|
|
7
|
-
*/
|
|
8
|
-
class Calculator
|
|
9
|
-
{
|
|
10
|
-
/**
|
|
11
|
-
* @param {@superhero/eventbus} eventbus
|
|
12
|
-
*/
|
|
13
|
-
constructor(eventbus)
|
|
14
|
-
{
|
|
15
|
-
this.eventbus = eventbus
|
|
16
|
-
this.calculations = []
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* @returns {number} the id of the created calculation
|
|
21
|
-
*/
|
|
22
|
-
createCalculation()
|
|
23
|
-
{
|
|
24
|
-
const id = this.calculations.push(0)
|
|
25
|
-
this.eventbus.emit('calculator.calculation-created', { id })
|
|
26
|
-
return id
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @throws {E_CALCULATION_COULD_NOT_BE_FOUND}
|
|
31
|
-
* @throws {E_INVALID_CALCULATION_TYPE}
|
|
32
|
-
*
|
|
33
|
-
* @param {CalculatorCalculation} dto
|
|
34
|
-
*
|
|
35
|
-
* @returns {number} the result of the calculation
|
|
36
|
-
*/
|
|
37
|
-
appendToCalculation({ id, type, value })
|
|
38
|
-
{
|
|
39
|
-
if(id < 1
|
|
40
|
-
|| id > this.calculations.length)
|
|
41
|
-
{
|
|
42
|
-
throw new CalculationCouldNotBeFoundError(`Id out of range: "${id}/${this.calculations.length}"`)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
switch(type)
|
|
46
|
-
{
|
|
47
|
-
case 'addition':
|
|
48
|
-
{
|
|
49
|
-
const result = this.calculations[id - 1] += value
|
|
50
|
-
this.eventbus.emit('calculator.calculation-appended', { id, type, result })
|
|
51
|
-
return result
|
|
52
|
-
}
|
|
53
|
-
case 'subtraction':
|
|
54
|
-
{
|
|
55
|
-
const result = this.calculations[id - 1] -= value
|
|
56
|
-
this.eventbus.emit('calculator.calculation-appended', { id, type, result })
|
|
57
|
-
return result
|
|
58
|
-
}
|
|
59
|
-
default:
|
|
60
|
-
{
|
|
61
|
-
throw new InvalidCalculationTypeError(`Unrecognized type used for calculation: "${type}"`)
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
module.exports = Calculator
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const
|
|
2
|
-
Calculator = require('.'),
|
|
3
|
-
LocatorConstituent = require('../../locator/constituent')
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @extends {@superhero/core/locator/constituent}
|
|
7
|
-
*/
|
|
8
|
-
class CalculatorLocator extends LocatorConstituent
|
|
9
|
-
{
|
|
10
|
-
/**
|
|
11
|
-
* @returns {Calculator}
|
|
12
|
-
*/
|
|
13
|
-
locate()
|
|
14
|
-
{
|
|
15
|
-
const eventbus = this.locator.locate('eventbus')
|
|
16
|
-
return new Calculator(eventbus)
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
module.exports = CalculatorLocator
|
package/test/init.js
DELETED
package/test/logger/config.js
DELETED
package/test/logger/index.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @implements {@superhero/eventbus/observer}
|
|
3
|
-
*/
|
|
4
|
-
class Logger
|
|
5
|
-
{
|
|
6
|
-
constructor(eventbus)
|
|
7
|
-
{
|
|
8
|
-
this.eventbus = eventbus
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
observe(event)
|
|
12
|
-
{
|
|
13
|
-
this.eventbus.emit('logger.logged-event', event)
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
module.exports = Logger
|
package/test/logger/locator.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const
|
|
2
|
-
Logger = require('.'),
|
|
3
|
-
LocatorConstituent = require('../../locator/constituent')
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @extends {@superhero/core/locator/constituent}
|
|
7
|
-
*/
|
|
8
|
-
class LoggerLocator extends LocatorConstituent
|
|
9
|
-
{
|
|
10
|
-
/**
|
|
11
|
-
* @returns {Logger}
|
|
12
|
-
*/
|
|
13
|
-
locate()
|
|
14
|
-
{
|
|
15
|
-
const eventbus = this.locator.locate('eventbus')
|
|
16
|
-
return new Logger(eventbus)
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
module.exports = LoggerLocator
|