Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
verkholantsev committed Mar 21, 2020
1 parent 4cbc3b5 commit e3f8346
Show file tree
Hide file tree
Showing 7 changed files with 4,147 additions and 3,185 deletions.
4 changes: 2 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"presets": [
"env",
"flow"
"@babel/env",
"@babel/flow"
]
}
3 changes: 2 additions & 1 deletion __tests__/overload.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

import 'regenerator-runtime/runtime';
import overload from '../src/overload';
import prettier from 'prettier';

const expectFormattedFnToMatchSnapshot = async fn => {
const config = Object.assign({ parser: 'babylon' }, await prettier.resolveConfig(__filename));
const config = Object.assign({ parser: 'babel' }, await prettier.resolveConfig(__filename));
expect(prettier.format(fn.toString(), config)).toMatchSnapshot();
};

Expand Down
5 changes: 4 additions & 1 deletion __tests__/pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import pair from '../src/pair';

describe('pair()', () => {
it('should transform array into array of pairs', () => {
expect(pair([0, 1, 2, 3])).toEqual([[0, 1], [2, 3]]);
expect(pair([0, 1, 2, 3])).toEqual([
[0, 1],
[2, 3],
]);
});
});
68 changes: 30 additions & 38 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
typeof exports === 'object' && typeof module !== 'undefined'
? (module.exports = factory())
: typeof define === 'function' && define.amd
? define(factory)
: (global.superoverload = factory());
? define(factory)
: ((global = global || self), (global.superoverload = factory()));
})(this, function() {
'use strict';

var TYPE_REGEX = /\s([a-zA-Z]+)/;

/**
* Returns type of `arg`
*/

function getType(arg) {
if (arg === null) {
return 'null';
Expand All @@ -22,7 +22,7 @@
var groups = Object.prototype.toString.call(arg).match(TYPE_REGEX);

if (!Array.isArray(groups)) {
throw new Error('Unexpected type of arg ' + String(arg));
throw new Error('Unexpected type of arg '.concat(String(arg)));
}

return groups[1].toLowerCase();
Expand Down Expand Up @@ -50,28 +50,28 @@
function serializeSignature(array) {
return array
.map(function(_, index) {
return '_' + index;
return '_'.concat(index);
})
.join(',');
}

var SUPPORTED_TYPES = toObject(['number', 'string', 'array', 'object', 'function', 'regexp', 'date']);

/**
* Creates object from array
*
* @private
*/

function toObject(elements) {
return elements.reduce(function(acc, element) {
acc[element] = true;
return acc;
}, {});
}

/**
* Returns unsupported types from function's signature
*/

function getUnsupportedTypes(signature) {
return signature.filter(function(type) {
return !SUPPORTED_TYPES[type];
Expand Down Expand Up @@ -108,14 +108,14 @@
* fn(1); // => 'It is a number'
* fn(''); // => 'It is something else'
*/

function overload() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

var defaultFn = args.length % 2 > 0 ? args.shift() : null;
var pairs = pair(args);

var ifsArray = new Array(pairs.length);
var fns = new Array(pairs.length);
var longestSignature = [];
Expand All @@ -124,15 +124,13 @@
var _pair = pairs[i];
var signature = _pair[0];
var fn = _pair[1];

var unsupportedTypes = getUnsupportedTypes(signature);

if (unsupportedTypes.length > 0) {
throw new Error(
'Signature "' +
signature.join(', ') +
'" contains unsupported types: "' +
unsupportedTypes.join(', ') +
'"'
'Signature "'
.concat(signature.join(', '), '" contains unsupported types: "')
.concat(unsupportedTypes.join(', '), '"')
);
}

Expand All @@ -143,33 +141,27 @@
longestSignature = signature;
}

ifsArray[i] =
"\nif (hashKey === '" +
hashKey +
"') {\n return fns[" +
String(i) +
'].call(this, ' +
serializeSignature(signature) +
');\n}';
ifsArray[i] = "\nif (hashKey === '"
.concat(hashKey, "') {\n return fns[")
.concat(String(i), '].call(this, ')
.concat(serializeSignature(signature), ');\n}');
}

var ifs = ifsArray.join(' else ');

var serializedSignature = serializeSignature(longestSignature);
var code =
'\nreturn function overloadedFn(' +
serializedSignature +
") {\n var hashKey = '';\n var len = arguments.length;\n var args = new Array(len);\n\n for (var i = 0; i < len; i++) {\n args[i] = arguments[i];\n }\n\n for (var i = 0; i < len; i++) {\n hashKey += getType(args[i]);\n if (i !== len - 1) {\n hashKey += ', ';\n }\n }\n " +
ifs +
'\n ' +
(pairs.length > 0 ? 'else {' : '') +
"\n if (!defaultFn) {\n throw new Error('No matching function for call with signature \"' + hashKey + '\"');\n }\n " +
(pairs.length > 0 ? '}' : '') +
'\n return defaultFn.apply(this, args);\n}';

var superFunc = new Function('getType, fns, defaultFn', code);

// $FlowFixMe errors with `new Function(...)`
var code = '\nreturn function overloadedFn('
.concat(
serializedSignature,
") {\n var hashKey = '';\n var len = arguments.length;\n var args = new Array(len);\n\n for (var i = 0; i < len; i++) {\n args[i] = arguments[i];\n }\n\n for (var i = 0; i < len; i++) {\n hashKey += getType(args[i]);\n if (i !== len - 1) {\n hashKey += ', ';\n }\n }\n "
)
.concat(ifs, '\n ')
.concat(
pairs.length > 0 ? 'else {' : '',
"\n if (!defaultFn) {\n throw new Error('No matching function for call with signature \"' + hashKey + '\"');\n }\n "
)
.concat(pairs.length > 0 ? '}' : '', '\n return defaultFn.apply(this, args);\n}');
var superFunc = new Function('getType, fns, defaultFn', code); // $FlowFixMe errors with `new Function(...)`

return superFunc(getType, fns, defaultFn);
}

Expand Down
38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@
"url": "git://github.com/verkholantsev/superoverload.git"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-env": "^1.6.1",
"babel-preset-flow": "^6.23.0",
"@babel/core": "^7.9.0",
"@babel/plugin-external-helpers": "^7.8.3",
"@babel/preset-env": "^7.9.0",
"@babel/preset-flow": "^7.9.0",
"babel-eslint": "^10.1.0",
"benchmark": "^2.1.4",
"captain-git-hook": "^2.0.0",
"coveralls": "^3.0.0",
"eslint": "^5.0.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-flowtype": "^2.45.0",
"eslint-plugin-prettier": "^2.6.0",
"flow-bin": "^0.76.0",
"jest": "^23.0.0",
"lodash": "^4.17.5",
"np": "^3.0.1",
"prettier": "^1.13.0",
"rollup": "^0.62.0",
"rollup-plugin-babel": "^3.0.3",
"rollup-plugin-commonjs": "^9.0.0",
"rollup-plugin-node-resolve": "^3.0.3"
"coveralls": "^3.0.11",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-flowtype": "^4.6.0",
"eslint-plugin-prettier": "^3.1.2",
"flow-bin": "^0.121.0",
"jest": "^25.1.0",
"lodash": "^4.17.15",
"np": "^6.2.0",
"prettier": "^1.19.1",
"rollup": "^2.1.0",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0"
},
"dependencies": {},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
},
plugins: [
babel({
presets: [['env', { modules: false }], 'flow'],
presets: [['@babel/env', { modules: false }], '@babel/flow'],
exclude: 'node_modules/**',
babelrc: false,
}),
Expand Down
Loading

0 comments on commit e3f8346

Please sign in to comment.