-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): 新增 devOrProd 开发环境和生产环境返回不同的值或调用不同的函数
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"name": "vscode-jest-tests", | ||
"request": "launch", | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen", | ||
"disableOptimisticBPs": true, | ||
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js", | ||
"cwd": "${workspaceFolder}", | ||
"args": ["--runInBand", "--watchAll=false"] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { devOrProd } from './devOrProd' | ||
|
||
describe('devOrProd', () => { | ||
beforeEach(() => { | ||
delete process.env.NODE_ENV | ||
}) | ||
|
||
test('空值被认为是生产环境', () => { | ||
delete process.env.NODE_ENV | ||
expect(devOrProd(1, 2)).toBe(2) | ||
process.env.NODE_ENV = '' | ||
expect(devOrProd(1, 2)).toBe(2) | ||
}) | ||
|
||
test('production', () => { | ||
process.env.NODE_ENV = 'production' | ||
expect(devOrProd(1, 2)).toBe(2) | ||
}) | ||
|
||
test('prod', () => { | ||
process.env.NODE_ENV = 'prod' | ||
expect(devOrProd(1, 2)).toBe(2) | ||
}) | ||
|
||
test('development', () => { | ||
process.env.NODE_ENV = 'development' | ||
expect(devOrProd(1, 2)).toBe(1) | ||
}) | ||
|
||
test('dev', () => { | ||
process.env.NODE_ENV = 'dev' | ||
expect(devOrProd(1, 2)).toBe(1) | ||
}) | ||
|
||
test('函数', () => { | ||
process.env.NODE_ENV = 'prod' | ||
expect(devOrProd(1, () => 2)).toBe(2) | ||
expect( | ||
devOrProd( | ||
() => 100, | ||
() => 4, | ||
), | ||
).toBe(4) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* 开发环境和生产环境返回不同的值或调用不同的函数。 | ||
* | ||
* `process.env.NODE_ENV` 为空值、`production`、`prod` 时被认为是生产环境,否则是开发环境。 | ||
* | ||
* @param devValue 开发环境返回的值或调用的函数 | ||
* @param prodValue 生产环境返回的值或调用的函数 | ||
*/ | ||
export function devOrProd<T>( | ||
devValue: T | (() => T), | ||
prodValue: T | (() => T), | ||
): T { | ||
const nodeEnv = process.env.NODE_ENV | ||
return !nodeEnv || nodeEnv === 'production' || nodeEnv === 'prod' | ||
? typeof prodValue === 'function' | ||
? (prodValue as any)() | ||
: prodValue | ||
: typeof devValue === 'function' | ||
? (devValue as any)() | ||
: devValue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters