Browse Source

[checkTypes]: add function

mightyplow 7 years ago
parent
commit
f561a2f882
4 changed files with 40 additions and 0 deletions
  1. 14 0
      src/function/__tests__/checkTypes.test.js
  2. 24 0
      src/function/checkTypes.js
  3. 1 0
      src/function/index.js
  4. 1 0
      src/index.js

+ 14 - 0
src/function/__tests__/checkTypes.test.js

@@ -0,0 +1,14 @@
+import checkTypes from '../checkTypes';
+
+export default test => {
+    test.ok(typeof checkTypes === 'function', 'checkTypes should be a function');
+
+    const values = [1, 'two', {}, [], null];
+    const types = ['number', 'string', Object, Array, null];
+    test.ok(checkTypes(values, types) === true, 'types should match');
+
+    const wrongTypes = ['number', 'string', Object, String, null];
+    test.ok(checkTypes(values, wrongTypes) === false, 'types should not match');
+
+    test.done();
+};

+ 24 - 0
src/function/checkTypes.js

@@ -0,0 +1,24 @@
+const checkType = (type) => (value) => {
+    // console.log(type, value);
+
+    if (type === null) {
+        return value === null;
+    } else if (typeof type === 'string') {
+        return typeof value === type;
+    } else if (typeof type === 'function') {
+        return value instanceof type;
+    } else if (typeof type === 'object') {
+        return type.isPrototypeOf(value);
+    }
+
+    return false;
+};
+
+/**
+ * @memberOf function
+ */
+const checkTypes = (values, types) => {
+    return values.every((value, index) => checkType(types[index])(value));
+};
+
+export default checkTypes;

+ 1 - 0
src/function/index.js

@@ -2,6 +2,7 @@
  * @namespace function
  */
 export { default as argsToArray } from './argsToArray';
+export { default as checkTypes } from './checkTypes';
 export { default as combine } from './combine';
 export { default as curry } from './curry';
 export { default as enqueue } from './enqueue';

+ 1 - 0
src/index.js

@@ -10,6 +10,7 @@ export {
 
 export {
     argsToArray,
+    checkTypes,
     combine,
     curry,
     enqueueWithResults,