Skip to content

Commit b5de522

Browse files
committed
refactor(*): change references to use flat lib structure
1 parent d0a0692 commit b5de522

File tree

5 files changed

+72
-50
lines changed

5 files changed

+72
-50
lines changed

index.js

+2-44
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,4 @@
1-
var BSON = require('./lib/bson/bson'),
2-
Binary = require('./lib/bson/binary'),
3-
Code = require('./lib/bson/code'),
4-
DBRef = require('./lib/bson/db_ref'),
5-
Decimal128 = require('./lib/bson/decimal128'),
6-
Double = require('./lib/bson/double'),
7-
Int32 = require('./lib/bson/int_32'),
8-
Long = require('./lib/bson/long'),
9-
Map = require('./lib/bson/map'),
10-
MaxKey = require('./lib/bson/max_key'),
11-
MinKey = require('./lib/bson/min_key'),
12-
ObjectId = require('./lib/bson/objectid'),
13-
BSONRegExp = require('./lib/bson/regexp'),
14-
Symbol = require('./lib/bson/symbol'),
15-
Timestamp = require('./lib/bson/timestamp');
1+
'use strict';
162

17-
// BSON MAX VALUES
18-
BSON.BSON_INT32_MAX = 0x7fffffff;
19-
BSON.BSON_INT32_MIN = -0x80000000;
20-
21-
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
22-
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
23-
24-
// JS MAX PRECISE VALUES
25-
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
26-
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
27-
28-
// Add BSON types to function creation
29-
BSON.Binary = Binary;
30-
BSON.Code = Code;
31-
BSON.DBRef = DBRef;
32-
BSON.Decimal128 = Decimal128;
33-
BSON.Double = Double;
34-
BSON.Int32 = Int32;
35-
BSON.Long = Long;
36-
BSON.Map = Map;
37-
BSON.MaxKey = MaxKey;
38-
BSON.MinKey = MinKey;
39-
BSON.ObjectId = ObjectId;
40-
BSON.ObjectID = ObjectId;
41-
BSON.BSONRegExp = BSONRegExp;
42-
BSON.Symbol = Symbol;
43-
BSON.Timestamp = Timestamp;
44-
45-
// Return the BSON
3+
const BSON = require('./lib/bson');
464
module.exports = BSON;

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@
4949
"config": {
5050
"native": false
5151
},
52-
"main": "./index",
53-
"directories": {
54-
"lib": "./lib/bson"
52+
"main": "index.js",
53+
"module": "dist/bson.esm.js",
54+
"browser": {
55+
"./index.js": "./dist/bson.browser.umd.js",
56+
"./dist/bson.esm.js": "./dist/bson.browser.esm.js"
5557
},
56-
"browser": "dist/bson.js",
5758
"engines": {
5859
"node": ">=4.0.0"
5960
},

test/node/bson_test.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const BinaryParser = require('../binary_parser').BinaryParser;
2020
const vm = require('vm');
2121
const assertBuffersEqual = require('./tools/utils').assertBuffersEqual;
2222
const createBSON = require('../utils');
23+
const normalizedFunctionString = require('../../lib/parser/utils').normalizedFunctionString;
2324

2425
// for tests
2526
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;

test/node/ensure_buffer_test.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const Buffer = require('buffer').Buffer;
4+
const ensureBuffer = require('../../lib/ensure_buffer');
5+
const expect = require('chai').expect;
6+
7+
describe('ensureBuffer tests', function() {
8+
it('should be a function', function() {
9+
expect(ensureBuffer).to.be.a('function');
10+
});
11+
12+
it('should return the exact same buffer if a buffer is passed in', function() {
13+
const bufferIn = new Buffer(10);
14+
let bufferOut;
15+
16+
expect(function() {
17+
bufferOut = ensureBuffer(bufferIn);
18+
}).to.not.throw(Error);
19+
20+
expect(bufferOut).to.equal(bufferIn);
21+
});
22+
23+
it('should wrap a UInt8Array with a buffer', function() {
24+
const arrayIn = Uint8Array.from([1, 2, 3]);
25+
let bufferOut;
26+
27+
expect(function() {
28+
bufferOut = ensureBuffer(arrayIn);
29+
}).to.not.throw(Error);
30+
31+
expect(bufferOut).to.be.an.instanceOf(Buffer);
32+
expect(bufferOut.buffer).to.equal(arrayIn.buffer);
33+
});
34+
35+
[0, 12, -1, '', 'foo', null, undefined, ['list'], {}, /x/].forEach(function(item) {
36+
it(`should throw if input is ${typeof item}: ${item}`, function() {
37+
expect(function() {
38+
ensureBuffer(item);
39+
}).to.throw(TypeError);
40+
});
41+
});
42+
43+
[
44+
/* eslint-disable */
45+
Int8Array,
46+
Uint8ClampedArray,
47+
Int16Array,
48+
Uint16Array,
49+
Int32Array,
50+
Uint32Array,
51+
Float32Array,
52+
Float64Array
53+
/* eslint-enable */
54+
].forEach(function(TypedArray) {
55+
it(`should throw if input is typed array ${TypedArray.name}`, function() {
56+
const typedArray = new TypedArray();
57+
expect(function() {
58+
ensureBuffer(typedArray);
59+
}).to.throw(TypeError);
60+
});
61+
});
62+
});

test/node/map_tests.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
var M = require('../../lib/bson/map');
4-
var createBSON = require('../utils');
3+
const M = require('../../lib/map');
4+
const createBSON = require('../utils');
55
const expect = require('chai').expect;
66

77
describe('Map', function() {

0 commit comments

Comments
 (0)