Skip to content

Commit

Permalink
Utility function and type for compat interop API (#4673)
Browse files Browse the repository at this point in the history
* compat interop util

* Create cyan-wasps-worry.md

* Update cyan-wasps-worry.md
  • Loading branch information
Feiyang1 authored Mar 24, 2021
1 parent fdadf71 commit de5f905
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
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 a utility function and type for compat interop API
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);
});
});

0 comments on commit de5f905

Please sign in to comment.