diff --git a/package.json b/package.json index b428bfc94..ebff92d13 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "syso", "license": "MIT", - "version": "0.6.2", + "version": "0.0.7", "description": "Library to compute the french social security contributions. Also a website that explains the calculations, and a generic engine to build similar simulators and computations.", "main": "./dist/engine.js", "files": ["dist/engine.js"], @@ -69,7 +69,8 @@ "test-common": "mocha-webpack --webpack-config source/webpack.test.js --require source-map-support/register --include componentTestSetup.js --require mock-local-storage --require test/helpers/browser.js \"./{,!(node_modules)/**/}!(webpack).test.js\"", "test": "yarn test-common", "test-components": "mocha-webpack --webpack-config source/webpack.test.js --require source-map-support/register --include componentTestSetup.js --require mock-local-storage ---require test/helpers/browser.js \"source/components/**/*.test.js\" --watch", - "test-lib": "yarn test-common --grep 'library'" + "test-lib": "yarn test-common --grep 'library'", + "compile-lib": "yarn webpack --config source/webpack.lib.js" }, "devDependencies": { "@babel/core": "^7.1.0", @@ -115,6 +116,7 @@ "http-server": "^0.11.1", "intl": "^1.2.5", "intl-locales-supported": "^1.0.0", + "isomorphic-style-loader": "^4.0.0", "js-yaml": "^3.12.0", "jsdom": "^12.0.0", "json-loader": "^0.5.7", @@ -132,7 +134,7 @@ "sinon": "^4.5.0", "sinon-chai": "^3.0.0", "source-map-support": "^0.5.4", - "style-loader": "^0.20.3", + "style-loader": "^0.23.1", "url-loader": "^1.0.1", "webpack": "^4.20.1", "webpack-cli": "^3.1.1", diff --git a/source/engine/index.js b/source/engine/index.js index 9d4e61c76..c33933ed5 100644 --- a/source/engine/index.js +++ b/source/engine/index.js @@ -19,10 +19,10 @@ let enrichRules = input => (typeof input === 'string' ? yaml.safeLoad(input) : input).map(enrichRule) export default { - evaluate: (targetInput, nestedSituation, rulesConfig) => { - let rules = rulesConfig + evaluate: (targetInput, nestedSituation, config) => { + let rules = config ? do { - let { base, extra } = rulesConfig + let { base, extra } = config ;[ ...(base ? enrichRules(base) : rulesFr), ...(extra ? enrichRules(extra) : []) @@ -35,7 +35,10 @@ export default { Array.isArray(targetInput) ? targetInput : [targetInput] )(nestedSituationToStateSelector(rules)(nestedSituation)) + if (config.debug) return evaluation + let values = evaluation.targets.map(t => t.nodeValue) + return Array.isArray(targetInput) ? values : values[0] } } diff --git a/source/webpack.common.js b/source/webpack.common.js index 8f2a0cf41..8e672164e 100644 --- a/source/webpack.common.js +++ b/source/webpack.common.js @@ -2,6 +2,7 @@ const HTMLPlugin = require('html-webpack-plugin') const CopyPlugin = require('copy-webpack-plugin') const path = require('path') +const { universal, web } = require('./webpack.commonLoaders.js') module.exports = { resolve: { @@ -21,70 +22,17 @@ module.exports = { infrance: ['./source/sites/mycompanyinfrance.fr/entry.js'], embauche: ['./source/sites/embauche.gouv.fr/entry.js'], // To not introduce breaking into the iframe integration, we serve simulateur.js from a 'dist' subdirectory - 'dist/simulateur': ['./source/sites/embauche.gouv.fr/iframe-script.js'], - engine: ['./source/engine/index.js'] + 'dist/simulateur': ['./source/sites/embauche.gouv.fr/iframe-script.js'] }, output: { path: path.resolve('./dist/'), filename: ({ chunk }) => - ['dist/simulateur', 'engine'].includes(chunk.name) + ['dist/simulateur'].includes(chunk.name) ? '[name].js' - : '[name].[hash].js', - libraryTarget: 'umd', - library: 'Syso' + : '[name].[hash].js' }, module: { - rules: [ - { - test: /\.css$/, - use: [ - { - loader: 'style-loader' - }, - { - loader: 'css-loader', - options: { - sourceMap: true, - importLoaders: 1 - } - }, - { - loader: 'postcss-loader' - } - ] - }, - { - test: /\.(jpe?g|png|svg)$/, - use: { - loader: 'file-loader', - options: { - name: 'images/[name].[ext]' - } - } - }, - { - test: /\.yaml$/, - loader: 'json-loader!yaml-loader' - }, - { - test: /\.js$/, - exclude: /node_modules|dist/, - loader: 'babel-loader' - }, - { - test: /\.csv$/, - loader: 'csv-loader', - options: { - dynamicTyping: true, - header: true, - skipEmptyLines: true - } - }, - { - test: /\.ne$/, - loader: 'babel-loader!nearley-loader' - } - ] + rules: [...web, ...universal] }, plugins: [ new HTMLPlugin({ diff --git a/source/webpack.commonLoaders.js b/source/webpack.commonLoaders.js new file mode 100644 index 000000000..7ac82e428 --- /dev/null +++ b/source/webpack.commonLoaders.js @@ -0,0 +1,44 @@ +module.exports = { + web: [ + { + test: /\.css$/, + use: [ + 'style-loader', + { + loader: 'css-loader', + options: { + sourceMap: true, + importLoaders: 1 + } + }, + { + loader: 'postcss-loader' + } + ] + } + ], + universal: [ + { + test: /\.(jpe?g|png|svg)$/, + use: { + loader: 'file-loader', + options: { + name: 'images/[name].[ext]' + } + } + }, + { + test: /\.yaml$/, + loader: 'json-loader!yaml-loader' + }, + { + test: /\.js$/, + exclude: /node_modules|dist/, + loader: 'babel-loader' + }, + { + test: /\.ne$/, + loader: 'babel-loader!nearley-loader' + } + ] +} diff --git a/source/webpack.lib.js b/source/webpack.lib.js new file mode 100644 index 000000000..432e62128 --- /dev/null +++ b/source/webpack.lib.js @@ -0,0 +1,38 @@ +const common = require('./webpack.common.js') +const { universal } = require('./webpack.commonLoaders.js') +const path = require('path') + +module.exports = { + resolve: common.resolve, + entry: './source/engine/index.js', + mode: 'development', + devtool: 'source-map', + output: { + path: path.resolve('./dist/'), + filename: 'engine.js', + library: 'Syso', + libraryTarget: 'umd', + globalObject: "(typeof window !== 'undefined' ? window : this)" + }, + module: { + rules: [ + ...universal, + { + test: /\.css$/, + use: [ + 'isomorphic-style-loader', + { + loader: 'css-loader', + options: { + sourceMap: true, + importLoaders: 1 + } + }, + { + loader: 'postcss-loader' + } + ] + } + ] + } +} diff --git a/test/library.test.js b/test/library.test.js index 36f106432..b3fcf7b3b 100644 --- a/test/library.test.js +++ b/test/library.test.js @@ -75,4 +75,58 @@ describe('library', function() { ) console.log({ revenuDisponible, dividendes }) }) + + it('temp', function() { + let règles = ` +- nom: revenu imposable + question: Quel est votre revenu imposable ? + format: euros + +- nom: revenu abattu + formule: + allègement: + assiette: revenu imposable + abattement: 10% + + +- nom: impôt sur le revenu + formule: + barème: + assiette: revenu abattu + tranches: + - en-dessous de: 9807 + taux: 0% + - de: 9807 + à: 27086 + taux: 14% + - de: 27086 + à: 72617 + taux: 30% + - de: 72617 + à: 153783 + taux: 41% + - au-dessus de: 153783 + taux: 45% + + +- nom: impôt sur le revenu à payer + formule: + allègement: + assiette: impôt sur le revenu + décote: + plafond: 1177 + taux: 75% +` + + let target = 'impôt sur le revenu à payer' + + let value = Syso.evaluate( + target, + { 'revenu imposable': '48000' }, + { extra: règles } + ) + console.log(value) + + expect(value).to.equal(7000) + }) }) diff --git a/yarn.lock b/yarn.lock index 154291f25..4e0ed177e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1512,7 +1512,7 @@ babel-polyfill@^6.26.0: core-js "^2.5.0" regenerator-runtime "^0.10.5" -babel-runtime@6.x, babel-runtime@^6.11.6, babel-runtime@^6.18.0, babel-runtime@^6.22.0, babel-runtime@^6.26.0: +babel-runtime@6.x, babel-runtime@^6.11.6, babel-runtime@^6.18.0, babel-runtime@^6.22.0, babel-runtime@^6.25.0, babel-runtime@^6.26.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" dependencies: @@ -4003,7 +4003,7 @@ hoek@4.x.x: version "4.2.1" resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.1.tgz#9634502aa12c445dd5a7c5734b572bb8738aacbb" -hoist-non-react-statics@^2.3.1, hoist-non-react-statics@^2.5.0, hoist-non-react-statics@^2.5.4: +hoist-non-react-statics@^2.2.2, hoist-non-react-statics@^2.3.1, hoist-non-react-statics@^2.5.0, hoist-non-react-statics@^2.5.4: version "2.5.5" resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.5.5.tgz#c5903cf409c0dfd908f388e619d86b9c1174cb47" @@ -4593,6 +4593,15 @@ isobject@^3.0.0, isobject@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df" +isomorphic-style-loader@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/isomorphic-style-loader/-/isomorphic-style-loader-4.0.0.tgz#0d8faa12885e2830918c54815042c0e3b55dc38d" + dependencies: + babel-runtime "^6.25.0" + hoist-non-react-statics "^2.2.2" + loader-utils "^1.1.0" + prop-types "^15.5.10" + isstream@~0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a" @@ -7753,12 +7762,12 @@ strip-json-comments@^2.0.1, strip-json-comments@~2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" -style-loader@^0.20.3: - version "0.20.3" - resolved "https://registry.yarnpkg.com/style-loader/-/style-loader-0.20.3.tgz#ebef06b89dec491bcb1fdb3452e913a6fd1c10c4" +style-loader@^0.23.1: + version "0.23.1" + resolved "https://registry.yarnpkg.com/style-loader/-/style-loader-0.23.1.tgz#cb9154606f3e771ab6c4ab637026a1049174d925" dependencies: loader-utils "^1.1.0" - schema-utils "^0.4.5" + schema-utils "^1.0.0" supports-color@5.1.0: version "5.1.0"