diff --git a/CHANGELOG.md b/CHANGELOG.md index 31aba47..9e42f5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## 3.1.3 - 2018-07-11 +### Changed +- refactoring of factory.js + ## 3.1.2 - 2018-05-04 ### Changed - `@rduk` modules as peerDependencies diff --git a/lib/factory.js b/lib/factory.js index 3743916..ac33c35 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -28,33 +28,27 @@ const errors = require('@rduk/errors') const configuration = require('@rduk/configuration') const Section = require('./section') -module.exports = function (name, base, defaultConfig) { - if (!name || typeof name !== 'string' || name.length === 0) { - errors.throwArgumentError('name', name) - } - - if (!base || !(base.prototype instanceof require('./base'))) { - errors.throwArgumentError('base', base) +class ProviderCreator { + constructor (name, Base, defaultConfig) { + this.name = name + this.Base = Base + this.defaultConfig = defaultConfig } - - let section, instance - - let getSection = function () { - if (!section) { - section = configuration.load().getSection(name, Section, !!defaultConfig) + getSection () { + if (!this._section) { + this._section = configuration.load().getSection(this.name, Section, !!this.defaultConfig) } - if (!section && !!defaultConfig) { - section = new Section(defaultConfig) + if (!this._section && !!this.defaultConfig) { + this._section = new Section(this.defaultConfig) } - return section + return this._section } - - let create = function (Provider, options) { + create (Provider, options, section) { let obj = new Provider(...[options, section]) - if (!(obj instanceof base)) { + if (!(obj instanceof this.Base)) { errors.throwConfigurationError('invalid provider type') } @@ -62,6 +56,10 @@ module.exports = function (name, base, defaultConfig) { return obj } +} + +const factory = creator => { + let instance return { getInstance: function () { @@ -72,10 +70,26 @@ module.exports = function (name, base, defaultConfig) { return instance }, get: function (name) { - let section = getSection() + let section = creator.getSection() let options = section.get(name) - return create(options.type, options, section) + return creator.create(options.type, options, section) } } } + +module.exports = (name, base, defaultConfig) => { + if (!name) { + errors.throwArgumentError('name', name) + } + + if (typeof name !== 'string') { + errors.throwArgumentError('name', name) + } + + if (!base || !(base.prototype instanceof require('./base'))) { + errors.throwArgumentError('base', base) + } + + return factory(new ProviderCreator(name, base, defaultConfig)) +} diff --git a/package-lock.json b/package-lock.json index 609d9d5..8b53426 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@rduk/provider", - "version": "3.1.1", + "version": "3.1.3", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -731,7 +731,7 @@ "js-yaml": "3.10.0", "json-stable-stringify-without-jsonify": "1.0.1", "levn": "0.3.0", - "lodash": "4.17.5", + "lodash": "4.17.9", "minimatch": "3.0.4", "mkdirp": "0.5.1", "natural-compare": "1.4.0", @@ -839,7 +839,7 @@ "eslint-import-resolver-node": "0.3.2", "eslint-module-utils": "2.2.0", "has": "1.0.1", - "lodash": "4.17.5", + "lodash": "4.17.9", "minimatch": "3.0.4", "read-pkg-up": "2.0.0" }, @@ -1359,7 +1359,7 @@ "cli-width": "2.2.0", "external-editor": "2.2.0", "figures": "2.0.0", - "lodash": "4.17.5", + "lodash": "4.17.9", "mute-stream": "0.0.7", "run-async": "2.3.0", "rx-lite": "4.0.8", @@ -1542,7 +1542,7 @@ "glob": { "version": "7.1.2", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", - "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", + "integrity": "sha1-wZyd+aAocC1nhhI4SmVSQExjbRU=", "dev": true, "requires": { "fs.realpath": "1.0.0", @@ -1698,9 +1698,9 @@ } }, "lodash": { - "version": "4.17.5", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.5.tgz", - "integrity": "sha512-svL3uiZf1RwhH+cWrfZn3A4+U58wbP0tGVTLQPbjplZxZ8ROD9VLuNgsRniTlLe7OlSqR79RUehXgpBW/s0IQw==" + "version": "4.17.9", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.9.tgz", + "integrity": "sha512-vuRLquvot5sKUldMBumG0YqLvX6m/RGBBOmqb3CWR/MC/QvvD1cTH1fOqxz2FJAQeoExeUdX5Gu9vP2EP6ik+Q==" }, "log-driver": { "version": "1.2.5", @@ -2550,7 +2550,7 @@ "ajv": "5.5.2", "ajv-keywords": "2.1.1", "chalk": "2.4.0", - "lodash": "4.17.5", + "lodash": "4.17.9", "slice-ansi": "1.0.0", "string-width": "2.1.1" } diff --git a/package.json b/package.json index 01fa1e2..945f6a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rduk/provider", - "version": "3.1.2", + "version": "3.1.3", "description": "Easily add providers to your Node.js app", "engines": { "node": ">=6.4.0" @@ -9,8 +9,9 @@ "scripts": { "pretest": "standard --fix && cp ./spec/resources/config*.yml .", "test": "istanbul cover node_modules/jasmine/bin/jasmine.js --report cobertura", - "posttest": "rm -rf ./config*yml && istanbul report", - "coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" + "posttest": "rm -rf ./config*yml", + "report": "istanbul report", + "coveralls": "npm run report && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" }, "repository": { "type": "git",