├── README.md ├── package.json └── index.js /README.md: -------------------------------------------------------------------------------- 1 | # bson-type-of-is 2 | 3 | ## Description 4 | This package is compatible with https://github.com/mongodb/js-bson/ 5 | 6 | ## BSON types 7 | https://docs.mongodb.com/manual/reference/bson-types/ 8 | 9 | ## Usage 10 | ``` 11 | import bson from 'bson' 12 | import bsonTypeOfIs from 'bson-type-of-is' 13 | 14 | const type = bsonTypeOfIs(bson.BSONPure.Long.fromNumber(10)) 15 | // type is 'long' 16 | ``` 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bson-type-of-is", 3 | "version": "1.3.2", 4 | "description": "Get BSON type alias of a value", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/poetic/bson-type-of-is.git" 12 | }, 13 | "author": "yang2007chun@gmail.com", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/poetic/bson-type-of-is/issues" 17 | }, 18 | "homepage": "https://github.com/poetic/bson-type-of-is#readme" 19 | } 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // BSON types 2 | // Type Number Alias 3 | // Double 1 “double” 4 | // String 2 “string” 5 | // Object 3 “object” 6 | // Array 4 “array” 7 | // BinaryData 5 “binData” 8 | // Undefined 6 “undefined” 9 | // ObjectId 7 “objectId” 10 | // Boolean 8 “bool” 11 | // Date 9 “date” 12 | // Null 10 “null” 13 | // RegularExpression 11 “regex” 14 | // DBPointer 12 “dbPointer” 15 | // JavaScript 13 “javascript” 16 | // Symbol 14 “symbol” 17 | // JavaScript(withScope) 15 “javascriptWithScope” 18 | // 32-bitInteger 16 “int” 19 | // Timestamp 17 “timestamp” 20 | // 64-bitInteger 18 “long” 21 | // MinKey -1 “minKey” 22 | // MaxKey 127 “maxKey” 23 | 24 | var TYPES = [ 25 | "undefined", 26 | "null", 27 | "double", 28 | "string", 29 | "array", 30 | "binData", 31 | "objectId", 32 | "bool", 33 | "date", 34 | "regex", 35 | "dbPointer", 36 | // "javascript", 37 | "symbol", 38 | // "javascriptWithScope", 39 | "int", 40 | "timestamp", 41 | "long", 42 | "object", 43 | // "minKey", 44 | // "maxKey", 45 | ] 46 | 47 | var typesArray = [ 48 | { 49 | alias: 'undefined', 50 | check: function (v) { return typeof v === 'undefined' } 51 | }, 52 | { 53 | alias: 'null', 54 | check: function (v) { return v === null } 55 | }, 56 | { 57 | alias: 'string', 58 | check: function (v) { return typeof v === 'string' } 59 | }, 60 | { 61 | alias: 'array', 62 | check: function (v) { return Array.isArray(v) } 63 | }, 64 | { 65 | alias: 'binData', 66 | check: function (v) { return v && v._bsontype === 'Binary' } 67 | }, 68 | { 69 | alias: 'objectId', 70 | check: function (v) { return v && v._bsontype === 'ObjectID' } 71 | }, 72 | { 73 | alias: 'bool', 74 | check: function (v) { return typeof v === 'boolean' } 75 | }, 76 | { 77 | alias: 'date', 78 | check: function (v) { return v instanceof Date } 79 | }, 80 | { 81 | alias: 'regex', 82 | check: function (v) { return v && v._bsontype === 'BSONRegExp' } 83 | }, 84 | { 85 | alias: 'dbPointer', 86 | check: function (v) { return v && v._bsontype === 'DBRef' } 87 | }, 88 | // { alias: 'javascript', check: function (v) { } }, 89 | { 90 | alias: 'symbol', 91 | check: function (v) { return v && v._bsontype === 'Symbol' } 92 | }, 93 | // { alias: 'javascriptWithScope', check: function (v) { return } }, 94 | { 95 | alias: 'int', 96 | check: function (v) { return v && v._bsontype === 'Int32' } 97 | }, 98 | { 99 | alias: 'timestamp', 100 | check: function (v) { return v && v._bsontype === 'Timestamp' } 101 | }, 102 | { 103 | alias: 'long', 104 | check: function (v) { return v && v._bsontype === 'Long' } 105 | }, 106 | // double should be after int and long 107 | { 108 | alias: 'double', 109 | check: function (v) { return typeof v === 'number' || (v && v._bsontype === 'Double') } 110 | }, 111 | // { alias: 'minKey', check: function (v) { return } }, 112 | // { alias: 'maxKey', check: function (v) { return } }, 113 | // NOTE: object should be at the end 114 | { 115 | alias: 'object', 116 | check: function (v) { return typeof v === 'object' } 117 | }, 118 | ] 119 | 120 | var typesObject = typesArray.reduce(function (acc, type) { 121 | acc[type.alias] = type.check 122 | return acc 123 | }, {}); 124 | 125 | // recognize bson types 126 | function bsonTypeOfIs (value) { 127 | var i 128 | for (i = 0; i < typesArray.length; i++) { 129 | if (typesArray[i].check(value)) { 130 | return typesArray[i].alias 131 | } 132 | } 133 | // Unrecognized type 134 | console.log('Value: ', value); 135 | throw new Error('The value above has a type we do not recognize'); 136 | } 137 | 138 | function testBsonType (type, value) { 139 | if (!typesObject[type]) { 140 | console.log('Value: ', value); 141 | throw new Error('The value above has a type we do not recognize'); 142 | } 143 | 144 | return typesObject[type].check(value) 145 | } 146 | 147 | bsonTypeOfIs.TYPES = TYPES 148 | bsonTypeOfIs.typesArray = typesArray 149 | bsonTypeOfIs.typesObject = typesObject 150 | bsonTypeOfIs.testBsonType = testBsonType 151 | 152 | module.exports = bsonTypeOfIs 153 | --------------------------------------------------------------------------------