Skip to content

Commit 3bc6405

Browse files
committed
chore: remove arrify package and support its replacement function
1 parent cab3f22 commit 3bc6405

File tree

7 files changed

+45
-16
lines changed

7 files changed

+45
-16
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
"@opentelemetry/semantic-conventions": "^1.30.0",
6666
"@types/big.js": "^6.2.2",
6767
"@types/stack-trace": "^0.0.33",
68-
"arrify": "2.0.0",
6968
"big.js": "^6.2.2",
7069
"checkpoint-stream": "^0.1.2",
7170
"duplexify": "^4.1.3",

src/codec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
import {GrpcService} from './common-grpc/service';
1717
import {PreciseDate} from '@google-cloud/precise-date';
18-
import arrify = require('arrify');
18+
import { toArray } from './helper';
1919
import {Big} from 'big.js';
2020
import * as is from 'is';
2121
import {common as p} from 'protobufjs';
@@ -1203,7 +1203,7 @@ function getType(value: Value): Type {
12031203
* @returns {object}
12041204
*/
12051205
function convertToListValue<T>(value: T): p.IListValue {
1206-
const values = (arrify(value) as T[]).map(codec.encode);
1206+
const values = (toArray(value) as T[]).map(codec.encode);
12071207
return {values};
12081208
}
12091209

@@ -1277,7 +1277,7 @@ function createTypeObject(
12771277

12781278
if (code === 'STRUCT') {
12791279
type.structType = {
1280-
fields: arrify(config.fields!).map(field => {
1280+
fields: toArray(config.fields!).map(field => {
12811281
return {name: field.name, type: codec.createTypeObject(field)};
12821282
}),
12831283
};

src/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ import {
9797
import {finished, Duplex, Readable, Transform} from 'stream';
9898
import {PreciseDate} from '@google-cloud/precise-date';
9999
import {EnumKey, RequestConfig, TranslateEnumKeys, Spanner} from '.';
100-
import arrify = require('arrify');
100+
import { toArray } from './helper';
101101
import {ServiceError} from 'google-gax';
102102
import IPolicy = google.iam.v1.IPolicy;
103103
import Policy = google.iam.v1.Policy;
@@ -4033,7 +4033,7 @@ class Database extends common.GrpcServiceObject {
40334033

40344034
if (typeof statements === 'string' || Array.isArray(statements)) {
40354035
statements = {
4036-
statements: arrify(statements) as string[],
4036+
statements: toArray(statements) as string[],
40374037
};
40384038
}
40394039
const reqOpts: databaseAdmin.spanner.admin.database.v1.IUpdateDatabaseDdlRequest =

src/helper.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,33 @@ export function isCreateSessionPermissionError(
8888
error.message.includes('spanner.sessions.create')
8989
);
9090
}
91+
92+
/**
93+
* Converts any value into an array. Acts as a replacement for `arrify`.
94+
*
95+
* - If the value is null or undefined, returns an empty array.
96+
* - If the value is already an array, returns is unchanges.
97+
* - Otherwise, wraps the value in a new array.
98+
*
99+
* @param value The value to convert into an array.
100+
* @returns An array containing the value, or an empty array.
101+
*/
102+
export function toArray(value: any) {
103+
if (value === null || value === undefined) {
104+
return [];
105+
}
106+
107+
if (Array.isArray(value)) {
108+
return value;
109+
}
110+
111+
if (typeof value === 'string') {
112+
return [value];
113+
}
114+
115+
if (typeof value[Symbol.iterator] === 'function') {
116+
return [...value];
117+
}
118+
119+
return [value];
120+
}

src/instance.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import arrify = require('arrify');
17+
import { toArray } from './helper';
1818
import {ServiceObjectConfig, GetConfig} from '@google-cloud/common';
1919
// eslint-disable-next-line @typescript-eslint/no-var-requires
2020
const common = require('./common-grpc/service-object');
@@ -912,7 +912,7 @@ class Instance extends common.GrpcServiceObject {
912912
delete reqOpts.gaxOptions;
913913

914914
if (reqOpts.schema) {
915-
reqOpts.extraStatements = arrify(reqOpts.schema);
915+
reqOpts.extraStatements = toArray(reqOpts.schema);
916916
delete reqOpts.schema;
917917
}
918918
this.request(
@@ -1536,7 +1536,7 @@ class Instance extends common.GrpcServiceObject {
15361536
};
15371537
if (options.fieldNames) {
15381538
reqOpts['fieldMask'] = {
1539-
paths: arrify(options['fieldNames']!).map(snakeCase),
1539+
paths: toArray(options['fieldNames']!).map(snakeCase),
15401540
};
15411541
}
15421542
return this.request<IInstance>(

src/transaction.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import {DateStruct, PreciseDate} from '@google-cloud/precise-date';
1818
import {promisifyAll} from '@google-cloud/promisify';
19-
import arrify = require('arrify');
19+
import { toArray } from './helper';
2020
import Long = require('long');
2121
import {EventEmitter} from 'events';
2222
import {grpc, CallOptions, ServiceError, Status, GoogleError} from 'google-gax';
@@ -1450,13 +1450,13 @@ export class Snapshot extends EventEmitter {
14501450
const keySet: spannerClient.spanner.v1.IKeySet = request.keySet || {};
14511451

14521452
if (request.keys) {
1453-
keySet.keys = arrify(request.keys as string[]).map(
1453+
keySet.keys = toArray(request.keys as string[]).map(
14541454
codec.convertToListValue,
14551455
);
14561456
}
14571457

14581458
if (request.ranges) {
1459-
keySet.ranges = arrify(request.ranges).map(range => {
1459+
keySet.ranges = toArray(request.ranges).map(range => {
14601460
const encodedRange: spannerClient.spanner.v1.IKeyRange = {};
14611461

14621462
Object.keys(range).forEach(bound => {
@@ -2805,7 +2805,7 @@ function buildMutation(
28052805
table: string,
28062806
keyVals: object | object[],
28072807
): spannerClient.spanner.v1.Mutation {
2808-
const rows: object[] = arrify(keyVals);
2808+
const rows: object[] = toArray(keyVals);
28092809
const columns = Transaction.getUniqueKeys(rows);
28102810

28112811
const values = rows.map((row, index) => {
@@ -2843,7 +2843,7 @@ function buildDeleteMutation(
28432843
keys: Key[],
28442844
): spannerClient.spanner.v1.Mutation {
28452845
const keySet: spannerClient.spanner.v1.IKeySet = {
2846-
keys: arrify(keys).map(codec.convertToListValue),
2846+
keys: toArray(keys).map(codec.convertToListValue),
28472847
};
28482848
const mutation: spannerClient.spanner.v1.IMutation = {
28492849
delete: {table, keySet},

test/instance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {Duplex} from 'stream';
2929

3030
import * as inst from '../src/instance';
3131
import {Spanner, Database, RequestConfig} from '../src';
32-
import arrify = require('arrify');
32+
import { toArray } from '../src/helper';
3333
import {SessionPoolOptions} from '../src/session-pool';
3434
import {Backup} from '../src/backup';
3535
import {PreciseDate} from '@google-cloud/precise-date';
@@ -1235,7 +1235,7 @@ describe('Instance', () => {
12351235
instance.request = config => {
12361236
assert.deepStrictEqual(config.reqOpts, {
12371237
fieldMask: {
1238-
paths: arrify(fieldNames).map(snakeCase),
1238+
paths: toArray(fieldNames).map(snakeCase),
12391239
},
12401240
name: instance.formattedName_,
12411241
});

0 commit comments

Comments
 (0)