diff --git a/src/entity.ts b/src/entity.ts index 6eb43bf19..0deefa5fe 100644 --- a/src/entity.ts +++ b/src/entity.ts @@ -51,12 +51,15 @@ entity.KEY_SYMBOL = Symbol('KEY'); * const datastore = new Datastore(); * const aDouble = datastore.double(7.3); */ -function Double(value) { - /** - * @name Double#value - * @type {number} - */ - this.value = value; +class Double { + value: number; + constructor(value: number) { + /** + * @name Double#value + * @type {number} + */ + this.value = value; + } } entity.Double = Double; @@ -85,12 +88,15 @@ entity.isDsDouble = isDsDouble; * const datastore = new Datastore(); * const anInt = datastore.int(7); */ -function Int(value) { - /** - * @name Int#value - * @type {string} - */ - this.value = value.toString(); +class Int { + value: string; + constructor(value: number|string) { + /** + * @name Int#value + * @type {string} + */ + this.value = value.toString(); + } } entity.Int = Int; @@ -108,6 +114,11 @@ function isDsInt(value) { entity.isDsInt = isDsInt; +export interface Coordinates { + latitude: number; + longitude: number; +} + /** * Build a Datastore Geo Point object. * @@ -126,16 +137,19 @@ entity.isDsInt = isDsInt; * * const geoPoint = datastore.geoPoint(coordinates); */ -function GeoPoint(coordinates) { - /** - * Coordinate value. - * - * @name GeoPoint#coordinates - * @type {object} - * @property {number} latitude Latitudinal value. - * @property {number} longitude Longitudinal value. - */ - this.value = coordinates; +class GeoPoint { + value: Coordinates; + constructor(coordinates: Coordinates) { + /** + * Coordinate value. + * + * @name GeoPoint#coordinates + * @type {object} + * @property {number} latitude Latitudinal value. + * @property {number} longitude Longitudinal value. + */ + this.value = coordinates; + } } entity.GeoPoint = GeoPoint; @@ -169,46 +183,55 @@ entity.isDsGeoPoint = isDsGeoPoint; * path: ['Company', 123] * }); */ -function Key(options) { - /** - * @name Key#namespace - * @type {string} - */ - this.namespace = options.namespace; - - options.path = [].slice.call(options.path); - - if (options.path.length % 2 === 0) { - const identifier = options.path.pop(); - - if (is.number(identifier) || isDsInt(identifier)) { - this.id = identifier.value || identifier; - } else if (is.string(identifier)) { - this.name = identifier; +class Key { + + namespace: string; + id?: string; + name?: string; + kind: string; + parent?: Key; + + constructor(options) { + /** + * @name Key#namespace + * @type {string} + */ + this.namespace = options.namespace; + + options.path = [].slice.call(options.path); + + if (options.path.length % 2 === 0) { + const identifier = options.path.pop(); + + if (is.number(identifier) || isDsInt(identifier)) { + this.id = identifier.value || identifier; + } else if (is.string(identifier)) { + this.name = identifier; + } } - } - this.kind = options.path.pop(); + this.kind = options.path.pop(); - if (options.path.length > 0) { - this.parent = new Key(options); - } + if (options.path.length > 0) { + this.parent = new Key(options); + } - // `path` is computed on demand to consider any changes that may have been - // made to the key. - /** - * @name Key#path - * @type {array} - */ - Object.defineProperty(this, 'path', { - enumerable: true, - get: function() { - return arrify(this.parent && this.parent.path).concat([ - this.kind, - this.name || this.id, - ]); - }, - }); + // `path` is computed on demand to consider any changes that may have been + // made to the key. + /** + * @name Key#path + * @type {array} + */ + Object.defineProperty(this, 'path', { + enumerable: true, + get: function() { + return arrify(this.parent && this.parent.path).concat([ + this.kind, + this.name || this.id, + ]); + }, + }); + } } entity.Key = Key; diff --git a/test/index.ts b/test/index.ts index d293e33fc..56a8ee7bc 100644 --- a/test/index.ts +++ b/test/index.ts @@ -75,12 +75,18 @@ const fakeGoogleGax = { }, }; -function FakeQuery() { - this.calledWith_ = arguments; +class FakeQuery { + calledWith_: IArguments; + constructor() { + this.calledWith_ = arguments; + } } -function FakeTransaction() { - this.calledWith_ = arguments; +class FakeTransaction { + calledWith_: IArguments; + constructor() { + this.calledWith_ = arguments; + } } function FakeV1() {} diff --git a/test/request.ts b/test/request.ts index 473973bf3..53b1266e8 100644 --- a/test/request.ts +++ b/test/request.ts @@ -48,9 +48,7 @@ const fakeV1 = { }, }; -function FakeQuery() { - this.calledWith_ = arguments; -} +class FakeQuery extends Query {} let pjyOverride; @@ -59,7 +57,7 @@ describe('Request', function() { let request; let key; - let sandbox; + const sandbox = sinon.createSandbox(); before(function() { Request = proxyquire('../src/request', { @@ -71,25 +69,21 @@ describe('Request', function() { }).DatastoreRequest; }); - after(function() { + after(() => { v1FakeClientOverride = null; }); beforeEach(function() { - sandbox = sinon.createSandbox(); pjyOverride = null; key = new entity.Key({ namespace: 'namespace', path: ['Company', 123], }); - FakeQuery.prototype = new Query(); v1FakeClientOverride = null; request = new Request(); }); - afterEach(() => { - sandbox.restore(); - }); + afterEach(() => sandbox.restore()); describe('instantiation', function() { it('should promisify all the things', function() { @@ -474,7 +468,7 @@ describe('Request', function() { request .createReadStream(key) .on('error', done) - .on('data', function(entity) { + .on('data', entity => { assert.deepStrictEqual(entity, expectedResult); }) .on('end', done) @@ -484,19 +478,18 @@ describe('Request', function() { it('should not push more results if stream was ended', function(done) { let entitiesEmitted = 0; - request.request_ = function(config, callback) { - setImmediate(function() { + request.request_ = (config, callback) => { + setImmediate(() => { callback(null, apiResponseWithMultiEntities); }); }; - request - .createReadStream([key, key]) - .on('data', function() { + const stream = request.createReadStream([key, key]); + stream.on('data', () => { entitiesEmitted++; - this.end(); + stream.end(); }) - .on('end', function() { + .on('end', () => { assert.strictEqual(entitiesEmitted, 1); done(); }) @@ -513,13 +506,11 @@ describe('Request', function() { }); }; - request - .createReadStream(key) + const stream = request.createReadStream(key); + stream .on('error', done) - .on('data', function() { - this.end(); - }) - .on('end', function() { + .on('data', () => stream.end()) + .on('end', () => { assert.strictEqual(lookupCount, 1); done(); }) @@ -930,16 +921,16 @@ describe('Request', function() { ); startCalled = true; return this; - }; + } - FakeQuery.prototype.offset = function(offset_) { + sandbox.stub(FakeQuery.prototype, 'offset').callsFake(offset_ => { const offset = query.offsetVal - apiResponse.batch.skippedResults; assert.strictEqual(offset_, offset); offsetCalled = true; return this; - }; + }); - FakeQuery.prototype.limit = function(limit_) { + sandbox.stub(FakeQuery.prototype, 'limit').callsFake(limit_ => { if (timesRequestCalled === 1) { assert.strictEqual( limit_, @@ -950,7 +941,7 @@ describe('Request', function() { assert.strictEqual(limit_, query.limitVal); } return this; - }; + }); sandbox.stub(entity, 'queryToQueryProto').callsFake(query_ => { if (timesRequestCalled > 1) { @@ -1012,10 +1003,10 @@ describe('Request', function() { sandbox.stub(entity, 'queryToQueryProto').returns({}); - FakeQuery.prototype.limit = function() { + sandbox.stub(FakeQuery.prototype, 'limit').callsFake(() => { limitCalled = true; return this; - }; + }); request .runQueryStream(query) @@ -1050,13 +1041,13 @@ describe('Request', function() { } }; - request + const stream = request .runQueryStream({}) - .on('data', function() { + .on('data', () => { entitiesEmitted++; - this.end(); + stream.end(); }) - .on('end', function() { + .on('end', () => { assert.strictEqual(entitiesEmitted, 1); done(); }); @@ -1070,13 +1061,11 @@ describe('Request', function() { callback(null, apiResponse); }; - request - .runQueryStream({}) + const stream = request.runQueryStream({}); + stream .on('error', done) - .on('data', function() { - this.end(); - }) - .on('end', function() { + .on('data', () => stream.end()) + .on('end', () => { assert.strictEqual(timesRequestCalled, 1); done(); }); diff --git a/tsconfig.json b/tsconfig.json index 1942ae08f..5dc9ad65f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,8 +3,7 @@ "compilerOptions": { "rootDir": ".", "outDir": "build", - "noImplicitAny": false, - "noImplicitThis": false + "noImplicitAny": false }, "include": [ "src/*.ts",