(feat) add argument --null-as-default-value to use null as default value for extracted translations
This commit is contained in:
		| @@ -105,3 +105,7 @@ Options: | |||||||
|                                                       [boolean] [default: false] |                                                       [boolean] [default: false] | ||||||
|   --key-as-default-value, -k   Use key as default value for translations |   --key-as-default-value, -k   Use key as default value for translations | ||||||
|                                                       [boolean] [default: false] |                                                       [boolean] [default: false] | ||||||
|  |   --null-as-default-value, -n  Use null as default value for translations | ||||||
|  |                                                       [boolean] [default: false] | ||||||
|  |  | ||||||
|  | Arguments key-as-default-value and null-as-default-value are mutually exclusive | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|   "name": "@biesbjerg/ngx-translate-extract", |   "name": "@biesbjerg/ngx-translate-extract", | ||||||
|   "version": "4.0.0", |   "version": "4.1.0", | ||||||
|   "description": "Extract strings from projects using ngx-translate", |   "description": "Extract strings from projects using ngx-translate", | ||||||
|   "main": "dist/index.js", |   "main": "dist/index.js", | ||||||
|   "typings": "dist/index.d.ts", |   "typings": "dist/index.d.ts", | ||||||
|   | |||||||
| @@ -10,6 +10,7 @@ import { MarkerParser } from '../parsers/marker.parser'; | |||||||
| import { PostProcessorInterface } from '../post-processors/post-processor.interface'; | import { PostProcessorInterface } from '../post-processors/post-processor.interface'; | ||||||
| import { SortByKeyPostProcessor } from '../post-processors/sort-by-key.post-processor'; | import { SortByKeyPostProcessor } from '../post-processors/sort-by-key.post-processor'; | ||||||
| import { KeyAsDefaultValuePostProcessor } from '../post-processors/key-as-default-value.post-processor'; | import { KeyAsDefaultValuePostProcessor } from '../post-processors/key-as-default-value.post-processor'; | ||||||
|  | import { NullAsDefaultValuePostProcessor } from '../post-processors/null-as-default-value.post-processor'; | ||||||
| import { PurgeObsoleteKeysPostProcessor } from '../post-processors/purge-obsolete-keys.post-processor'; | import { PurgeObsoleteKeysPostProcessor } from '../post-processors/purge-obsolete-keys.post-processor'; | ||||||
| import { CompilerInterface } from '../compilers/compiler.interface'; | import { CompilerInterface } from '../compilers/compiler.interface'; | ||||||
| import { CompilerFactory } from '../compilers/compiler.factory'; | import { CompilerFactory } from '../compilers/compiler.factory'; | ||||||
| @@ -87,6 +88,13 @@ export const cli = yargs | |||||||
| 		default: false, | 		default: false, | ||||||
| 		type: 'boolean' | 		type: 'boolean' | ||||||
| 	}) | 	}) | ||||||
|  | 	.option('null-as-default-value', { | ||||||
|  | 		alias: 'n', | ||||||
|  | 		describe: 'Use null as default value for translations', | ||||||
|  | 		default: false, | ||||||
|  | 		type: 'boolean' | ||||||
|  | 	}) | ||||||
|  | 	.conflicts('key-as-default-value', 'null-as-default-value') | ||||||
| 	.exitProcess(true) | 	.exitProcess(true) | ||||||
| 	.parse(process.argv); | 	.parse(process.argv); | ||||||
|  |  | ||||||
| @@ -111,6 +119,8 @@ if (cli.clean) { | |||||||
| } | } | ||||||
| if (cli.keyAsDefaultValue) { | if (cli.keyAsDefaultValue) { | ||||||
| 	postProcessors.push(new KeyAsDefaultValuePostProcessor()); | 	postProcessors.push(new KeyAsDefaultValuePostProcessor()); | ||||||
|  | } else if (cli.nullAsDefaultValue) { | ||||||
|  | 	postProcessors.push(new NullAsDefaultValuePostProcessor()); | ||||||
| } | } | ||||||
| if (cli.sort) { | if (cli.sort) { | ||||||
| 	postProcessors.push(new SortByKeyPostProcessor()); | 	postProcessors.push(new SortByKeyPostProcessor()); | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								src/post-processors/null-as-default-value.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/post-processors/null-as-default-value.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | |||||||
|  | import { TranslationCollection } from '../utils/translation.collection'; | ||||||
|  | import { PostProcessorInterface } from './post-processor.interface'; | ||||||
|  |  | ||||||
|  | export class NullAsDefaultValuePostProcessor implements PostProcessorInterface { | ||||||
|  |  | ||||||
|  | 	public name: string = 'NullAsDefaultValue'; | ||||||
|  |  | ||||||
|  | 	public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection { | ||||||
|  | 		return draft.map((key, val) => existing.get(key) === undefined ? null : val); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -0,0 +1,42 @@ | |||||||
|  | import { expect } from 'chai'; | ||||||
|  |  | ||||||
|  | import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface'; | ||||||
|  | import { NullAsDefaultValuePostProcessor } from '../../src/post-processors/null-as-default-value.post-processor'; | ||||||
|  | import { TranslationCollection } from '../../src/utils/translation.collection'; | ||||||
|  |  | ||||||
|  | describe('NullAsDefaultValuePostProcessor', () => { | ||||||
|  |  | ||||||
|  | 	let processor: PostProcessorInterface; | ||||||
|  |  | ||||||
|  | 	beforeEach(() => { | ||||||
|  | 		processor = new NullAsDefaultValuePostProcessor(); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	it('should use null as default value', () => { | ||||||
|  | 		const draft = new TranslationCollection({ 'String A': '' }); | ||||||
|  | 		const extracted = new TranslationCollection({ 'String A': '' }); | ||||||
|  | 		const existing = new TranslationCollection(); | ||||||
|  | 		expect(processor.process(draft, extracted, existing).values).to.deep.equal({ | ||||||
|  | 			'String A': null | ||||||
|  | 		}); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	it('should keep existing value even if it is an empty string', () => { | ||||||
|  | 		const draft = new TranslationCollection({ 'String A': '' }); | ||||||
|  | 		const extracted = new TranslationCollection({ 'String A': '' }); | ||||||
|  | 		const existing = new TranslationCollection({ 'String A': '' }); | ||||||
|  | 		expect(processor.process(draft, extracted, existing).values).to.deep.equal({ | ||||||
|  | 			'String A': '' | ||||||
|  | 		}); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	it('should keep existing value', () => { | ||||||
|  | 		const draft = new TranslationCollection({ 'String A': 'Streng A' }); | ||||||
|  | 		const extracted = new TranslationCollection({ 'String A': 'Streng A' }); | ||||||
|  | 		const existing = new TranslationCollection({ 'String A': 'Streng A' }); | ||||||
|  | 		expect(processor.process(draft, extracted, existing).values).to.deep.equal({ | ||||||
|  | 			'String A': 'Streng A' | ||||||
|  | 		}); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | }); | ||||||
		Reference in New Issue
	
	Block a user