Skip to content

Commit

Permalink
Use a es5 main file to unwrap the typescript default. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
mertenvg committed Jul 20, 2018
1 parent 02ccd76 commit 483dfdf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
5 changes: 5 additions & 0 deletions dist/es5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const casey = require('./');

module.exports = casey.default;
27 changes: 15 additions & 12 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Type declaration for a case condition function that receives the value to match against and returns a boolean
*/
type ConditionFunction = (value?: any) => boolean;
type ConditionFunction = ( value?: any ) => boolean;

/**
* Type declaration for a case value function that returns the desired value when a case is matched
Expand All @@ -12,7 +12,7 @@ type ValueFunction = () => any;
* Interface declares the structure needed for a case.
*/
interface Case {
condition: (value: any) => boolean;
condition: ( value: any ) => boolean;
value: () => any;
}

Expand All @@ -28,7 +28,7 @@ class Casey {
*
* @param {[ConditionFunction | any, ValueFunction | any][]} cases
*/
constructor(cases?: [ConditionFunction | any, ValueFunction | any][]) {
constructor( cases?: [ConditionFunction | any, ValueFunction | any][] ) {
if (cases) {
this.map(cases);
}
Expand All @@ -41,7 +41,7 @@ class Casey {
*
* @param {[ConditionFunction | any, ValueFunction | any][]} cases
*/
public map(cases: [ConditionFunction | any, ValueFunction | any][]) {
public map( cases: [ConditionFunction | any, ValueFunction | any][] ) {
if (cases && cases.forEach && cases.length) {
cases.forEach(c => this.case(c[0], c[1]));
}
Expand All @@ -61,8 +61,8 @@ class Casey {
typeof condition === "function"
? condition
: typeof condition === "boolean"
? (v: any) => condition
: (v: any) => v === condition;
? ( v: any ) => condition
: ( v: any ) => v === condition;
const valueFunc = typeof value === "function" ? value : () => value;

this.cases.push({ condition: conditionFunc, value: valueFunc });
Expand All @@ -74,7 +74,7 @@ class Casey {
*
* @param {ValueFunction | any} value The value to be returned when no match is found or a function that returns the desired default value.
*/
public default(value: ValueFunction | any): Casey {
public default( value: ValueFunction | any ): Casey {
this.defaultValue = () => value;
return this;
}
Expand All @@ -84,19 +84,19 @@ class Casey {
*
* @param {any} value The value to match against or be given to your condition function for custom matching.
*/
public match(value?: any): any {
public match( value?: any ): any {
if (value === undefined) {
return this.defaultValue();
}
const found = this.cases.find((c: Case) => {
const found = this.cases.find(( c: Case ) => {
return c.condition(value);
});
return found ? found.value() : this.defaultValue();
}
}

export default {
map: (cases: [ConditionFunction | any, ValueFunction | any][]): Casey => {
const instance = {
map: ( cases: [ConditionFunction | any, ValueFunction | any][] ): Casey => {
return new Casey(cases);
},
case: (
Expand All @@ -105,7 +105,10 @@ export default {
): Casey => {
return new Casey([[condition, value]]);
},
default: (value: ValueFunction | any): Casey => {
default: ( value: ValueFunction | any ): Casey => {
return new Casey().default(value);
}

};

export = instance;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "A javascript functional approach to switch and if-else statements",
"access": "public",
"main": "dist/index.js",
"main": "dist/es5.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
Expand Down

0 comments on commit 483dfdf

Please sign in to comment.