Skip to content

Commit

Permalink
removed fua.core.util dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerdie committed Nov 12, 2024
1 parent 9e8482f commit 1a5d2be
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 68 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "@nrd/fua.module.dfc",
"version": "1.0.0",
"version": "1.0.1",
"description": "Data Flow Control",
"main": "src/module.dfc.js",
"files": [
"src/*"
],
"dependencies": {
"@nrd/fua.core.util": "^1.0.0"
"@nrd/fua.core.assert": "^1.0.2",
"@nrd/fua.core.is": "^1.0.7"
},
"devDependencies": {
"mocha": "^8.2.1",
Expand All @@ -27,4 +28,4 @@
},
"author": "[email protected]",
"license": "UNLICENSED"
}
}
14 changes: 5 additions & 9 deletions src/module.dfc.CSVTransformer.js → src/CSVTransformer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const
util = require('./module.dfc.util.js'),
Transformer = require('./module.dfc.Transformer.js');

function CSVTransformer(options = {}) {
is = require('@nrd/fua.core.is'),
Transformer = require('./Transformer.js');

module.exports = function CSVTransformer(options = {}) {
const
transformer = Transformer(options.id || ''),
headers = !!(options.headers),
Expand All @@ -15,7 +14,7 @@ function CSVTransformer(options = {}) {
stripBom = !!(options.stripBom);

transformer.use(function (source, output, next) {
if (!util.isString(source))
if (!is.string(source))
return next(new Error('expected input to be a string'));
next(null, {header: null, rows: []});
});
Expand Down Expand Up @@ -65,7 +64,4 @@ function CSVTransformer(options = {}) {
});

return transformer;

} // CSVTransformer

module.exports = CSVTransformer;
}
22 changes: 22 additions & 0 deletions src/JSONTransformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const
assert = require('@nrd/fua.core.assert'),
is = require('@nrd/fua.core.is'),
Transformer = require('./Transformer.js');

module.exports = function JSONTransformer(options = {}) {
const transformer = Transformer(options.id || '');

transformer.use(function (source, output, next) {
try {
if (is.object(source)) source = JSON.stringify(source);
assert(is.string(source), 'expected input to be a string');
output = JSON.parse(source);
assert(is.object(output), 'expected output to be json');
next(null, output);
} catch (err) {
next(err);
}
});

return transformer;
}
21 changes: 10 additions & 11 deletions src/module.dfc.Transformer.js → src/Transformer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const util = require('./module.dfc.util.js');
const
assert = require('@nrd/fua.core.assert'),
is = require('@nrd/fua.core.is');

function Transformer(id = '') {
util.assert(util.isString(id), `Transformer : expected id to be a string`, TypeError);
module.exports = function Transformer(id = '') {
assert(is.string(id), `Transformer : expected id to be a string`, TypeError);

let
transformMethods = [],
Expand All @@ -10,16 +12,16 @@ function Transformer(id = '') {
async function transformer(source, output = source, next) {
for (let method of transformMethods) {
output = await new Promise((resolve, reject) => {
const next = (err, result) => err ? reject(err) : resolve(util.isDefined(result) ? result : output);
const next = (err, result) => err ? reject(err) : resolve(is.defined(result) ? result : output);
method.call(transformer, source, output, next);
});
}
return util.isFunction(next) ? next(output) : output;
return is.function(next) ? next(output) : output;
} // transformer

function useMethod(...methods) {
util.assert(!changeLocked, `Transformer<${id}>.use : already locked`);
util.assert(methods.every(util.isFunction), `Transformer<${id}>.use : expected method to be a function`, TypeError);
assert(!changeLocked, `Transformer<${id}>.use : already locked`);
assert(methods.every(is.function), `Transformer<${id}>.use : expected method to be a function`, TypeError);
transformMethods.push(...methods);
return transformer;
} // useMethod
Expand All @@ -36,7 +38,4 @@ function Transformer(id = '') {
});

return transformer;

} // Transformer

module.exports = Transformer;
}
5 changes: 5 additions & 0 deletions src/dfc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.Transformer = require('./Transformer.js');
exports.CSVTransformer = require('./CSVTransformer.js');
exports.JSONTransformer = require('./JSONTransformer.js');

Object.freeze(exports);
29 changes: 0 additions & 29 deletions src/module.dfc.JSONTransformer.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/module.dfc.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/module.dfc.util.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/Transformer.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const
{describe, test} = require('mocha'),
expect = require('expect'),
dfc = require('../src/module.dfc.js');
dfc = require('../src/dfc.js');

describe('module.dfc.Transformer', function () {

Expand Down

0 comments on commit 1a5d2be

Please sign in to comment.