Comparing version
interface IOptions { | ||
default: any; | ||
} | ||
declare function one(variable: any, object: object | undefined, options?: IOptions): any; | ||
declare const _default: { | ||
interface ISwitchObject { | ||
case: (variable: any) => boolean; | ||
value: any; | ||
} | ||
declare function one(variable: any, switchObjectOrArray: object | ISwitchObject[] | undefined, options?: IOptions): any; | ||
declare const fn: { | ||
one: typeof one; | ||
switch: typeof one; | ||
}; | ||
export default _default; | ||
export default fn; |
20
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function one(variable, object, options) { | ||
if ((typeof object === 'object' && object !== null)) { | ||
for (var _i = 0, _a = Object.entries(object); _i < _a.length; _i++) { | ||
function one(variable, switchObjectOrArray, options) { | ||
if ((typeof switchObjectOrArray === 'object' && switchObjectOrArray !== null)) { | ||
for (var _i = 0, _a = Object.entries(switchObjectOrArray); _i < _a.length; _i++) { | ||
var _b = _a[_i], key = _b[0], value = _b[1]; | ||
@@ -12,7 +12,13 @@ if (key == variable) { | ||
} | ||
if (Array.isArray(switchObjectOrArray) && switchObjectOrArray.length) { | ||
for (var _c = 0, switchObjectOrArray_1 = switchObjectOrArray; _c < switchObjectOrArray_1.length; _c++) { | ||
var switchObject = switchObjectOrArray_1[_c]; | ||
if (typeof switchObject.case === 'function' && switchObject.case(variable)) { | ||
return switchObject.value; | ||
} | ||
} | ||
} | ||
return options === null || options === void 0 ? void 0 : options.default; | ||
} | ||
exports.default = { | ||
one: one, | ||
switch: one | ||
}; | ||
var fn = { one: one, switch: one }; | ||
exports.default = fn; |
{ | ||
"name": "fn-code", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "Functions (fn) to make code cleaner", | ||
@@ -10,3 +10,4 @@ "main": "index.js", | ||
"test": "jest", | ||
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls" | ||
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls", | ||
"g": "yarn tsc && cp ./dist/index.js index.js && yarn jest" | ||
}, | ||
@@ -13,0 +14,0 @@ "files": [ |
@@ -115,2 +115,26 @@ # Fn functions | ||
But what if you want to pass a regex or a more specific comparison than `case == value` ? | ||
No problem! You can define the switch as an array of **case functions** and values! | ||
(These case functions must always return boolean) | ||
````typescript | ||
const binomalName = fn.one(animal, [ | ||
{ | ||
case: (str: string) => str.includes('cat'), | ||
value: 'Felis catus' | ||
}, | ||
{ | ||
case: (str: string) => str.includes('lion'), | ||
value: 'Panthera leo' | ||
}, | ||
{ | ||
case: (str: string) => str.includes('dog'), | ||
value: 'Canis familiaris' | ||
} | ||
], { default: 'Species not found' }) | ||
```` | ||
Alternatively, if it feels more familiar you can use `fn.switch` instead, as it is an alias for `fn.one`. | ||
@@ -117,0 +141,0 @@ |
8694
17.12%36
38.46%166
16.9%