Skip to content
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

Utility function and type for compat interop API #4673

Merged
merged 3 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cyan-wasps-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/util": patch
---

Added an utility function and type for compat interop API
Feiyang1 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/util/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ export * from './src/validation';
export * from './src/utf8';
export * from './src/exponential_backoff';
export * from './src/formatters';
export * from './src/compat';
1 change: 1 addition & 0 deletions packages/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export * from './src/validation';
export * from './src/utf8';
export * from './src/exponential_backoff';
export * from './src/formatters';
export * from './src/compat';
30 changes: 30 additions & 0 deletions packages/util/src/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @license
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export interface Compat<T> {
_delegate: T;
}

export function getModularInstance<ExpService>(
service: Compat<ExpService> | ExpService
): ExpService {
if (service && (service as Compat<ExpService>)._delegate) {
return (service as Compat<ExpService>)._delegate;
} else {
return service as ExpService;
}
}
35 changes: 35 additions & 0 deletions packages/util/test/compat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from 'chai';
import { getModularInstance } from '../src/compat';

describe('getModularInstance()', () => {
it('returns _delegate from a compat object', () => {
const compat = {
_delegate: {}
};
const modularThing = getModularInstance(compat);
expect(modularThing).to.eq(compat._delegate);
});

it('returns the object itself if it is not a compat object', () => {
const thing = {};
const modularThing = getModularInstance(thing);
expect(modularThing).to.eq(thing);
});
});