coerce.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Generated by LiveScript 1.2.0
  2. (function(){
  3. var parsedTypeCheck, types, toString$ = {}.toString;
  4. parsedTypeCheck = require('type-check').parsedTypeCheck;
  5. types = {
  6. '*': function(it){
  7. switch (toString$.call(it).slice(8, -1)) {
  8. case 'Array':
  9. return coerceType(it, {
  10. type: 'Array'
  11. });
  12. case 'Object':
  13. return coerceType(it, {
  14. type: 'Object'
  15. });
  16. default:
  17. return {
  18. type: 'Just',
  19. value: coerceTypes(it, [
  20. {
  21. type: 'Undefined'
  22. }, {
  23. type: 'Null'
  24. }, {
  25. type: 'NaN'
  26. }, {
  27. type: 'Boolean'
  28. }, {
  29. type: 'Number'
  30. }, {
  31. type: 'Date'
  32. }, {
  33. type: 'RegExp'
  34. }, {
  35. type: 'Array'
  36. }, {
  37. type: 'Object'
  38. }, {
  39. type: 'String'
  40. }
  41. ], {
  42. explicit: true
  43. })
  44. };
  45. }
  46. },
  47. Undefined: function(it){
  48. if (it === 'undefined' || it === void 8) {
  49. return {
  50. type: 'Just',
  51. value: void 8
  52. };
  53. } else {
  54. return {
  55. type: 'Nothing'
  56. };
  57. }
  58. },
  59. Null: function(it){
  60. if (it === 'null') {
  61. return {
  62. type: 'Just',
  63. value: null
  64. };
  65. } else {
  66. return {
  67. type: 'Nothing'
  68. };
  69. }
  70. },
  71. NaN: function(it){
  72. if (it === 'NaN') {
  73. return {
  74. type: 'Just',
  75. value: NaN
  76. };
  77. } else {
  78. return {
  79. type: 'Nothing'
  80. };
  81. }
  82. },
  83. Boolean: function(it){
  84. if (it === 'true') {
  85. return {
  86. type: 'Just',
  87. value: true
  88. };
  89. } else if (it === 'false') {
  90. return {
  91. type: 'Just',
  92. value: false
  93. };
  94. } else {
  95. return {
  96. type: 'Nothing'
  97. };
  98. }
  99. },
  100. Number: function(it){
  101. return {
  102. type: 'Just',
  103. value: +it
  104. };
  105. },
  106. Int: function(it){
  107. return {
  108. type: 'Just',
  109. value: parseInt(it)
  110. };
  111. },
  112. Float: function(it){
  113. return {
  114. type: 'Just',
  115. value: parseFloat(it)
  116. };
  117. },
  118. Date: function(value, options){
  119. var that;
  120. if (that = /^\#(.*)\#$/.exec(value)) {
  121. return {
  122. type: 'Just',
  123. value: new Date(+that[1] || that[1])
  124. };
  125. } else if (options.explicit) {
  126. return {
  127. type: 'Nothing'
  128. };
  129. } else {
  130. return {
  131. type: 'Just',
  132. value: new Date(+value || value)
  133. };
  134. }
  135. },
  136. RegExp: function(value, options){
  137. var that;
  138. if (that = /^\/(.*)\/([gimy]*)$/.exec(value)) {
  139. return {
  140. type: 'Just',
  141. value: new RegExp(that[1], that[2])
  142. };
  143. } else if (options.explicit) {
  144. return {
  145. type: 'Nothing'
  146. };
  147. } else {
  148. return {
  149. type: 'Just',
  150. value: new RegExp(value)
  151. };
  152. }
  153. },
  154. Array: function(it){
  155. return coerceArray(it, {
  156. of: [{
  157. type: '*'
  158. }]
  159. });
  160. },
  161. Object: function(it){
  162. return coerceFields(it, {
  163. of: {}
  164. });
  165. },
  166. String: function(it){
  167. var that;
  168. if (toString$.call(it).slice(8, -1) !== 'String') {
  169. return {
  170. type: 'Nothing'
  171. };
  172. }
  173. if (that = it.match(/^'(.*)'$/)) {
  174. return {
  175. type: 'Just',
  176. value: that[1]
  177. };
  178. } else if (that = it.match(/^"(.*)"$/)) {
  179. return {
  180. type: 'Just',
  181. value: that[1]
  182. };
  183. } else {
  184. return {
  185. type: 'Just',
  186. value: it
  187. };
  188. }
  189. }
  190. };
  191. function coerceArray(node, type){
  192. var typeOf, element;
  193. if (toString$.call(node).slice(8, -1) !== 'Array') {
  194. return {
  195. type: 'Nothing'
  196. };
  197. }
  198. typeOf = type.of;
  199. return {
  200. type: 'Just',
  201. value: (function(){
  202. var i$, ref$, len$, results$ = [];
  203. for (i$ = 0, len$ = (ref$ = node).length; i$ < len$; ++i$) {
  204. element = ref$[i$];
  205. results$.push(coerceTypes(element, typeOf));
  206. }
  207. return results$;
  208. }())
  209. };
  210. }
  211. function coerceTuple(node, type){
  212. var result, i$, ref$, len$, i, types, that;
  213. if (toString$.call(node).slice(8, -1) !== 'Array') {
  214. return {
  215. type: 'Nothing'
  216. };
  217. }
  218. result = [];
  219. for (i$ = 0, len$ = (ref$ = type.of).length; i$ < len$; ++i$) {
  220. i = i$;
  221. types = ref$[i$];
  222. if (that = coerceTypes(node[i], types)) {
  223. result.push(that);
  224. }
  225. }
  226. return {
  227. type: 'Just',
  228. value: result
  229. };
  230. }
  231. function coerceFields(node, type){
  232. var typeOf, key, value;
  233. if (toString$.call(node).slice(8, -1) !== 'Object') {
  234. return {
  235. type: 'Nothing'
  236. };
  237. }
  238. typeOf = type.of;
  239. return {
  240. type: 'Just',
  241. value: (function(){
  242. var ref$, results$ = {};
  243. for (key in ref$ = node) {
  244. value = ref$[key];
  245. results$[key] = coerceTypes(value, typeOf[key] || [{
  246. type: '*'
  247. }]);
  248. }
  249. return results$;
  250. }())
  251. };
  252. }
  253. function coerceType(node, typeObj, options){
  254. var type, structure, coerceFunc;
  255. type = typeObj.type, structure = typeObj.structure;
  256. if (type) {
  257. coerceFunc = types[type];
  258. return coerceFunc(node, options);
  259. } else {
  260. switch (structure) {
  261. case 'array':
  262. return coerceArray(node, typeObj);
  263. case 'tuple':
  264. return coerceTuple(node, typeObj);
  265. case 'fields':
  266. return coerceFields(node, typeObj);
  267. }
  268. }
  269. }
  270. function coerceTypes(node, types, options){
  271. var i$, len$, type, ref$, valueType, value;
  272. for (i$ = 0, len$ = types.length; i$ < len$; ++i$) {
  273. type = types[i$];
  274. ref$ = coerceType(node, type, options), valueType = ref$.type, value = ref$.value;
  275. if (valueType === 'Nothing') {
  276. continue;
  277. }
  278. if (parsedTypeCheck([type], value)) {
  279. return value;
  280. }
  281. }
  282. throw new Error("Value " + JSON.stringify(node) + " does not type check against " + JSON.stringify(types) + ".");
  283. }
  284. module.exports = coerceTypes;
  285. }).call(this);