enum-fp
Version:
Functional enum type for javascript with simple pattern matching
1 lines • 3.5 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:true});exports.values=exports.isObject=exports.isList=exports.createEnumFactory=exports.prop=exports.Constructor=void 0;function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};var ownKeys=Object.keys(source);if(typeof Object.getOwnPropertySymbols==="function"){ownKeys=ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym){return Object.getOwnPropertyDescriptor(source,sym).enumerable}))}ownKeys.forEach(function(key){_defineProperty(target,key,source[key])})}return target}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}function _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(iter){if(Symbol.iterator in Object(iter)||Object.prototype.toString.call(iter)==="[object Arguments]")return Array.from(iter)}function _arrayWithoutHoles(arr){if(Array.isArray(arr)){for(var i=0,arr2=new Array(arr.length);i<arr.length;i++){arr2[i]=arr[i]}return arr2}}function identity(x){return x}var Constructor=identity;exports.Constructor=Constructor;var prop=function prop(path,defaultVal){return function(obj){return path.reduce(function(o,key){return(o||{}).hasOwnProperty(key)?o[key]:defaultVal},obj)}};exports.prop=prop;var normalizeSumType=function normalizeSumType(sumType){return isList(sumType)?sumType.map(function(name){return Constructor({name:name})}):Object.keys(sumType).map(function(name){return Constructor({name:name,props:sumType[name]})})};var match=function match(instance,pattern){if(!instance||!instance.name)throw new Error("Invalid instance passed");var action=pattern[instance.name]||pattern._;if(!action)throw new Error("Non-Exhaustive pattern. You must pass fallback case `_` in the pattern");return action.apply(void 0,_toConsumableArray(instance.args))};var listToObject=function listToObject(toKey,toValue,list){return list.reduce(function(obj,item){return _objectSpread({},obj,_defineProperty({},toKey(item),toValue(item)))},{})};var createEnumFactory=function createEnumFactory(options){return function(sumTypeBody){var constructors=normalizeSumType(sumTypeBody);var createConstructor=options.createConstructor;var typeNames=constructors.map(prop(["name"]));var isConstructor=function isConstructor(t){return!t?false:typeNames.indexOf(t)!==-1||typeNames.indexOf(t.name)!==-1};var cata=function cata(pattern){return function(instance){return match(instance,pattern)}};var self={isConstructor:isConstructor,match:match,cata:cata,caseOf:cata,reduce:cata,constructors:listToObject(prop(["name"]),identity,constructors),length:constructors.length,forEach:constructors.forEach.bind(constructors)};return _objectSpread({},listToObject(prop(["name"]),function(constr){return createConstructor(self,constr)},constructors),self)}};exports.createEnumFactory=createEnumFactory;var isObjectOfType=function isObjectOfType(typeName){return function(a){return{}.toString.call(a)==="[object ".concat(typeName,"]")}};var isList=isObjectOfType("Array");exports.isList=isList;var isObject=isObjectOfType("Object");exports.isObject=isObject;var values=function values(obj){return Object.keys(obj).sort().map(function(k){return obj[k]})};exports.values=values;
;