ngx-thaibahtpipe
Version:
Angular pipe that transforms number into Thai words
154 lines (148 loc) • 4.95 kB
JavaScript
import { Pipe, NgModule } from '@angular/core';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class ThaiBahtPipe {
constructor() {
this.thaiNum = ['ศูนย์', 'หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า'];
this.thaiUnit = ['', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน'];
}
/**
* @param {?} value
* @param {?=} args
* @return {?}
*/
transform(value, args) {
if (isNaN(value) || value === null) {
return value;
}
/** @type {?} */
const splitArray = parseFloat(value).toFixed(2).toString().split('.');
/** @type {?} */
const bahtArray = splitArray[0].replace('-', '')
.split('')
.reverse()
.map((/**
* @param {?} val
* @return {?}
*/
(val) => parseInt(val, 10)));
/** @type {?} */
const resultBahtArray = [];
resultBahtArray.push('บาท');
/** @type {?} */
const resultBaht = this.transformArray(bahtArray);
resultBahtArray.push(resultBaht.reverse().join(''));
if (splitArray[1] === undefined || splitArray[1] === '00') {
return resultBahtArray.reverse().join('') + 'ถ้วน';
}
/** @type {?} */
const satangArray = splitArray[1].replace('-', '')
.split('')
.reverse()
.map((/**
* @param {?} val
* @return {?}
*/
(val) => parseInt(val, 10)));
/** @type {?} */
const resultSatangArray = [];
resultSatangArray.push('สตางค์');
/** @type {?} */
const resultSatang = this.transformArray(satangArray).join('');
resultSatangArray.push(resultSatang);
return resultBahtArray.reverse().join('') + resultSatangArray.reverse().join('');
}
/**
* @private
* @param {?} array
* @return {?}
*/
transformArray(array) {
return this.chunkArray(array, 6).map((/**
* @param {?} chunk
* @param {?} chunkIndex
* @return {?}
*/
(chunk, chunkIndex) => {
/** @type {?} */
const chunkResult = [];
if (chunkIndex > 0) {
chunkResult.push('ล้าน');
}
chunk.forEach((/**
* @param {?} num
* @param {?} index
* @return {?}
*/
(num, index) => {
if (num === 0 && chunk[index + 1] !== undefined) {
return;
}
if (num === 1 && index === 0 && chunk.length === 2 && chunk[1] === 0) {
chunkResult.push('หนึ่ง');
return;
}
if (num === 1 && index === 0 && chunk.length > 1) {
chunkResult.push('เอ็ด');
return;
}
if (num === 1 && index === 1) {
chunkResult.push(this.thaiUnit[index]);
return;
}
if (num === 2 && index === 1) {
chunkResult.push('ยี่' + this.thaiUnit[index]);
return;
}
if (num === 0 && index !== 0) {
return;
}
chunkResult.push(this.thaiNum[num] + this.thaiUnit[index]);
}));
return chunkResult.reverse().join('');
}));
}
/**
* @private
* @param {?} array
* @param {?} size
* @return {?}
*/
chunkArray(array, size) {
/** @type {?} */
const results = [];
while (array.length) {
results.push(array.splice(0, size));
}
return results;
}
}
ThaiBahtPipe.decorators = [
{ type: Pipe, args: [{
name: 'thaibaht'
},] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class ThaiBahtPipeModule {
}
ThaiBahtPipeModule.decorators = [
{ type: NgModule, args: [{
declarations: [ThaiBahtPipe],
exports: [ThaiBahtPipe]
},] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { ThaiBahtPipe, ThaiBahtPipeModule };
//# sourceMappingURL=ngx-thaibahtpipe.js.map