Skip to content

Commit

Permalink
fix(deps): drop dependency on is (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Apr 1, 2021
1 parent f1e35e4 commit f32da13
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ system-test/*key.json
.DS_Store
package-lock.json
__pycache__
.vscode
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"gcp-metadata": "^4.0.0",
"google-auth-library": "^7.0.0",
"google-gax": "^2.9.2",
"is": "^3.3.0",
"on-finished": "^2.3.0",
"pumpify": "^2.0.1",
"snakecase-keys": "^3.1.2",
Expand All @@ -70,7 +69,6 @@
"@google-cloud/pubsub": "^2.0.0",
"@google-cloud/storage": "^5.0.0",
"@types/extend": "^3.0.1",
"@types/is": "0.0.21",
"@types/mocha": "^8.0.0",
"@types/mv": "^2.1.0",
"@types/ncp": "^2.0.3",
Expand Down
28 changes: 14 additions & 14 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

import * as is from 'is';

export interface ObjectToStructConverterConfig {
removeCircular?: boolean;
stringify?: boolean;
Expand Down Expand Up @@ -76,7 +74,7 @@ export class ObjectToStructConverter {
for (const prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
const value = obj[prop];
if (is.undefined(value)) {
if (value === undefined) {
continue;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -105,27 +103,35 @@ export class ObjectToStructConverter {
encodeValue_(value: {} | null): any {
let convertedValue;

if (is.null(value)) {
if (value === null) {
convertedValue = {
nullValue: 0,
};
} else if (is.number(value)) {
} else if (typeof value === 'number') {
convertedValue = {
numberValue: value,
};
} else if (is.string(value)) {
} else if (typeof value === 'string') {
convertedValue = {
stringValue: value,
};
} else if (is.boolean(value)) {
} else if (typeof value === 'boolean') {
convertedValue = {
boolValue: value,
};
} else if (Buffer.isBuffer(value)) {
convertedValue = {
blobValue: value,
};
} else if (is.object(value)) {
} else if (Array.isArray(value)) {
convertedValue = {
listValue: {
values: (value as Array<{}>).map(this.encodeValue_.bind(this)),
},
};
} else if (value?.toString() === '[object Object]') {
// Using `typeof value === 'object'` is discouraged here, because it
// return true for dates, nulls, arrays, etc.
if (this.seenObjects.has(value!)) {
// Circular reference.
if (!this.removeCircular) {
Expand All @@ -144,12 +150,6 @@ export class ObjectToStructConverter {
structValue: this.convert(value!),
};
}
} else if (is.array(value)) {
convertedValue = {
listValue: {
values: (value as Array<{}>).map(this.encodeValue_.bind(this)),
},
};
} else {
if (!this.stringify) {
throw new Error('Value of type ' + typeof value + ' not recognized.');
Expand Down
13 changes: 6 additions & 7 deletions src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const EventId = require('eventid');
import * as extend from 'extend';
import * as is from 'is';
import {google} from '../protos/protos';
import {objToStruct, structToObj} from './common';

Expand Down Expand Up @@ -153,25 +152,25 @@ class Entry {
toJSON(options: ToJsonOptions = {}) {
const entry = (extend(true, {}, this.metadata) as {}) as EntryJson;
// Format log message
if (is.object(this.data)) {
if (this.data?.toString() === '[object Object]') {
entry.jsonPayload = objToStruct(this.data, {
removeCircular: !!options.removeCircular,
stringify: true,
});
} else if (is.string(this.data)) {
} else if (typeof this.data === 'string') {
entry.textPayload = this.data;
}
// Format log timestamp
if (is.date(entry.timestamp)) {
const seconds = (entry.timestamp as Date).getTime() / 1000;
if (entry.timestamp instanceof Date) {
const seconds = entry.timestamp.getTime() / 1000;
const secondsRounded = Math.floor(seconds);
entry.timestamp = {
seconds: secondsRounded,
nanos: Math.floor((seconds - secondsRounded) * 1e9),
};
} else if (is.string(entry.timestamp)) {
} else if (typeof entry.timestamp === 'string') {
// Convert RFC3339 "Zulu" timestamp into a format that can be parsed to Date
const zuluTime = entry.timestamp as string;
const zuluTime = entry.timestamp;
const ms = Date.parse(zuluTime.split(/[.,Z]/)[0] + 'Z');
const reNano = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.(\d{0,9})Z$/;
const nanoSecs = zuluTime.match(reNano)?.[1];
Expand Down

0 comments on commit f32da13

Please sign in to comment.