RobinHerbots / Inputmask

Input Mask plugin
https://robinherbots.github.io/Inputmask/
MIT License
6.41k stars 2.16k forks source link

Numeric abbreviations support #1842

Open laurent-addstones opened 6 years ago

laurent-addstones commented 6 years ago

Hello, I was wondering if it was possible to get inputmask to support abbreviations such as 6k (six thousand) or 3m (three million) using available options and methods ?

If not, how (where) could I modify the existing code to add this feature ?

Thanks

RobinHerbots commented 6 years ago

@laurent-addstones ,

What exactly do you expect when typing say 6k? That is expands to 6000?

You can already experiment with (only add k ~try typing 6k)

  Inputmask("numeric", {
            definitions: {
                "~": {
                    validator: function (chrs, maskset, pos, strict, opts, isSelection) {
                        if (chrs === "k") {
                            return {
                                insert: [{
                                    pos: pos,
                                    c: 0
                                }, {
                                    pos: pos + 1,
                                    c: 0
                                }],
                                pos: pos + 2,
                                c: 0
                            }
                        }
                        var isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
                        if (isValid === true) {
                            if (opts.numericInput !== true && maskset.validPositions[pos] !== undefined && maskset.validPositions[pos].match.def === "~" && !isSelection) {
                                var processValue = maskset.buffer.join("");
                                //strip negation symbol
                                processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
                                processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
                                //filter 0 after radixpoint
                                var pvRadixSplit = processValue.split(opts.radixPoint);
                                if (pvRadixSplit.length > 1) {
                                    pvRadixSplit[1] = pvRadixSplit[1].replace(/0/g, opts.placeholder.charAt(0));
                                }
                                //filter 0 before radixpoint
                                if (pvRadixSplit[0] === "0") {
                                    pvRadixSplit[0] = pvRadixSplit[0].replace(/0/g, opts.placeholder.charAt(0));
                                }
                                processValue = pvRadixSplit[0] + opts.radixPoint + pvRadixSplit[1] || "";
                                var bufferTemplate = maskset._buffer.join(""); //getBuffer().slice(lvp).join('');
                                if (processValue === opts.radixPoint) {
                                    processValue = bufferTemplate;
                                }
                                while (processValue.match(Inputmask.escapeRegex(bufferTemplate) + "$") === null) {
                                    bufferTemplate = bufferTemplate.slice(1);
                                }
                                // if (processValue !== opts.radixPoint) {
                                processValue = processValue.replace(bufferTemplate, "");
                                // }
                                processValue = processValue.split("");

                                if (processValue[pos] === undefined) {
                                    isValid = {
                                        "pos": pos,
                                        "remove": pos
                                    };
                                } else {
                                    isValid = {
                                        pos: pos
                                    };
                                }
                            }
                        } else if (!strict && chrs === opts.radixPoint && maskset.validPositions[pos - 1] === undefined) {
                            isValid = {
                                insert: {
                                    pos: pos,
                                    c: 0
                                },
                                pos: pos + 1
                            }
                        }
                        return isValid;
                    },
                    cardinality: 1
                }
            }
        }).mask("input");
laurent-addstones commented 6 years ago

Yes that's what I meant. Thank you !