Skip to content

Commit de5f905

Browse files
authored
Utility function and type for compat interop API (#4673)
* compat interop util * Create cyan-wasps-worry.md * Update cyan-wasps-worry.md
1 parent fdadf71 commit de5f905

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

.changeset/cyan-wasps-worry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/util": patch
3+
---
4+
5+
Added a utility function and type for compat interop API

packages/util/index.node.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ export * from './src/validation';
3737
export * from './src/utf8';
3838
export * from './src/exponential_backoff';
3939
export * from './src/formatters';
40+
export * from './src/compat';

packages/util/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ export * from './src/validation';
3232
export * from './src/utf8';
3333
export * from './src/exponential_backoff';
3434
export * from './src/formatters';
35+
export * from './src/compat';

packages/util/src/compat.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
export interface Compat<T> {
19+
_delegate: T;
20+
}
21+
22+
export function getModularInstance<ExpService>(
23+
service: Compat<ExpService> | ExpService
24+
): ExpService {
25+
if (service && (service as Compat<ExpService>)._delegate) {
26+
return (service as Compat<ExpService>)._delegate;
27+
} else {
28+
return service as ExpService;
29+
}
30+
}

packages/util/test/compat.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect } from 'chai';
19+
import { getModularInstance } from '../src/compat';
20+
21+
describe('getModularInstance()', () => {
22+
it('returns _delegate from a compat object', () => {
23+
const compat = {
24+
_delegate: {}
25+
};
26+
const modularThing = getModularInstance(compat);
27+
expect(modularThing).to.eq(compat._delegate);
28+
});
29+
30+
it('returns the object itself if it is not a compat object', () => {
31+
const thing = {};
32+
const modularThing = getModularInstance(thing);
33+
expect(modularThing).to.eq(thing);
34+
});
35+
});

0 commit comments

Comments
 (0)