UNPKG

ngx-thaibahtpipe

Version:

Angular pipe that transforms number into Thai words

175 lines (169 loc) 5.58 kB
import { Pipe, NgModule } from '@angular/core'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var ThaiBahtPipe = /** @class */ (function () { function ThaiBahtPipe() { this.thaiNum = ['ศูนย์', 'หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า']; this.thaiUnit = ['', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน']; } /** * @param {?} value * @param {?=} args * @return {?} */ ThaiBahtPipe.prototype.transform = /** * @param {?} value * @param {?=} args * @return {?} */ function (value, args) { if (isNaN(value) || value === null) { return value; } /** @type {?} */ var splitArray = parseFloat(value).toFixed(2).toString().split('.'); /** @type {?} */ var bahtArray = splitArray[0].replace('-', '') .split('') .reverse() .map((/** * @param {?} val * @return {?} */ function (val) { return parseInt(val, 10); })); /** @type {?} */ var resultBahtArray = []; resultBahtArray.push('บาท'); /** @type {?} */ var resultBaht = this.transformArray(bahtArray); resultBahtArray.push(resultBaht.reverse().join('')); if (splitArray[1] === undefined || splitArray[1] === '00') { return resultBahtArray.reverse().join('') + 'ถ้วน'; } /** @type {?} */ var satangArray = splitArray[1].replace('-', '') .split('') .reverse() .map((/** * @param {?} val * @return {?} */ function (val) { return parseInt(val, 10); })); /** @type {?} */ var resultSatangArray = []; resultSatangArray.push('สตางค์'); /** @type {?} */ var resultSatang = this.transformArray(satangArray).join(''); resultSatangArray.push(resultSatang); return resultBahtArray.reverse().join('') + resultSatangArray.reverse().join(''); }; /** * @private * @param {?} array * @return {?} */ ThaiBahtPipe.prototype.transformArray = /** * @private * @param {?} array * @return {?} */ function (array) { var _this = this; return this.chunkArray(array, 6).map((/** * @param {?} chunk * @param {?} chunkIndex * @return {?} */ function (chunk, chunkIndex) { /** @type {?} */ var chunkResult = []; if (chunkIndex > 0) { chunkResult.push('ล้าน'); } chunk.forEach((/** * @param {?} num * @param {?} index * @return {?} */ function (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 {?} */ ThaiBahtPipe.prototype.chunkArray = /** * @private * @param {?} array * @param {?} size * @return {?} */ function (array, size) { /** @type {?} */ var results = []; while (array.length) { results.push(array.splice(0, size)); } return results; }; ThaiBahtPipe.decorators = [ { type: Pipe, args: [{ name: 'thaibaht' },] } ]; return ThaiBahtPipe; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var ThaiBahtPipeModule = /** @class */ (function () { function ThaiBahtPipeModule() { } ThaiBahtPipeModule.decorators = [ { type: NgModule, args: [{ declarations: [ThaiBahtPipe], exports: [ThaiBahtPipe] },] } ]; return ThaiBahtPipeModule; }()); /** * @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