Skip to content

chore!: remove the arrify package #2292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@octokit/rest": "^21.0.0",
"@octokit/rest": "^19.0.0",
"mocha": "^10.0.0",
"sinon": "^18.0.0"
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"@opentelemetry/semantic-conventions": "^1.30.0",
"@types/big.js": "^6.2.2",
"@types/stack-trace": "^0.0.33",
"arrify": "2.0.0",
"big.js": "^7.0.0",
"checkpoint-stream": "^0.1.2",
"duplexify": "^4.1.3",
Expand Down
6 changes: 3 additions & 3 deletions src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import {GrpcService} from './common-grpc/service';
import {PreciseDate} from '@google-cloud/precise-date';
import arrify = require('arrify');
import {toArray} from './helper';
import {Big} from 'big.js';
import * as is from 'is';
import {common as p} from 'protobufjs';
Expand Down Expand Up @@ -1203,7 +1203,7 @@ function getType(value: Value): Type {
* @returns {object}
*/
function convertToListValue<T>(value: T): p.IListValue {
const values = (arrify(value) as T[]).map(codec.encode);
const values = (toArray(value) as T[]).map(codec.encode);
return {values};
}

Expand Down Expand Up @@ -1277,7 +1277,7 @@ function createTypeObject(

if (code === 'STRUCT') {
type.structType = {
fields: arrify(config.fields!).map(field => {
fields: toArray(config.fields!).map(field => {
return {name: field.name, type: codec.createTypeObject(field)};
}),
};
Expand Down
4 changes: 2 additions & 2 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ import {
import {finished, Duplex, Readable, Transform} from 'stream';
import {PreciseDate} from '@google-cloud/precise-date';
import {EnumKey, RequestConfig, TranslateEnumKeys, Spanner} from '.';
import arrify = require('arrify');
import {toArray} from './helper';
import {ServiceError} from 'google-gax';
import IPolicy = google.iam.v1.IPolicy;
import Policy = google.iam.v1.Policy;
Expand Down Expand Up @@ -4033,7 +4033,7 @@ class Database extends common.GrpcServiceObject {

if (typeof statements === 'string' || Array.isArray(statements)) {
statements = {
statements: arrify(statements) as string[],
statements: toArray(statements) as string[],
};
}
const reqOpts: databaseAdmin.spanner.admin.database.v1.IUpdateDatabaseDdlRequest =
Expand Down
28 changes: 28 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,31 @@ export function isCreateSessionPermissionError(
error.message.includes('spanner.sessions.create')
);
}

/**
* Converts any value into an array. Acts as a replacement for `arrify`.
* If the value is null or undefined, returns an empty array.
* If the value is already an array, returns is unchanges.
* Otherwise, wraps the value in a new array.
* @param value The value to convert into an array.
* @returns An array containing the value, or an empty array.
*/
export function toArray(value: any) {
if (value === null || value === undefined) {
return [];
}

if (Array.isArray(value)) {
return value;
}

if (typeof value === 'string') {
return [value];
}

if (typeof value[Symbol.iterator] === 'function') {
return [...value];
}

return [value];
}
6 changes: 3 additions & 3 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

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

if (reqOpts.schema) {
reqOpts.extraStatements = arrify(reqOpts.schema);
reqOpts.extraStatements = toArray(reqOpts.schema);
delete reqOpts.schema;
}
this.request(
Expand Down Expand Up @@ -1536,7 +1536,7 @@ class Instance extends common.GrpcServiceObject {
};
if (options.fieldNames) {
reqOpts['fieldMask'] = {
paths: arrify(options['fieldNames']!).map(snakeCase),
paths: toArray(options['fieldNames']!).map(snakeCase),
};
}
return this.request<IInstance>(
Expand Down
10 changes: 5 additions & 5 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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

if (request.keys) {
keySet.keys = arrify(request.keys as string[]).map(
keySet.keys = toArray(request.keys as string[]).map(
codec.convertToListValue,
);
}

if (request.ranges) {
keySet.ranges = arrify(request.ranges).map(range => {
keySet.ranges = toArray(request.ranges).map(range => {
const encodedRange: spannerClient.spanner.v1.IKeyRange = {};

Object.keys(range).forEach(bound => {
Expand Down Expand Up @@ -2805,7 +2805,7 @@ function buildMutation(
table: string,
keyVals: object | object[],
): spannerClient.spanner.v1.Mutation {
const rows: object[] = arrify(keyVals);
const rows: object[] = toArray(keyVals);
const columns = Transaction.getUniqueKeys(rows);

const values = rows.map((row, index) => {
Expand Down Expand Up @@ -2843,7 +2843,7 @@ function buildDeleteMutation(
keys: Key[],
): spannerClient.spanner.v1.Mutation {
const keySet: spannerClient.spanner.v1.IKeySet = {
keys: arrify(keys).map(codec.convertToListValue),
keys: toArray(keys).map(codec.convertToListValue),
};
const mutation: spannerClient.spanner.v1.IMutation = {
delete: {table, keySet},
Expand Down
4 changes: 2 additions & 2 deletions test/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {Duplex} from 'stream';

import * as inst from '../src/instance';
import {Spanner, Database, RequestConfig} from '../src';
import arrify = require('arrify');
import {toArray} from '../src/helper';
import {SessionPoolOptions} from '../src/session-pool';
import {Backup} from '../src/backup';
import {PreciseDate} from '@google-cloud/precise-date';
Expand Down Expand Up @@ -1235,7 +1235,7 @@ describe('Instance', () => {
instance.request = config => {
assert.deepStrictEqual(config.reqOpts, {
fieldMask: {
paths: arrify(fieldNames).map(snakeCase),
paths: toArray(fieldNames).map(snakeCase),
},
name: instance.formattedName_,
});
Expand Down
Loading