Add Snake Case Input Preset

Please add to Input Preset new preset type as Snake, to format input field as snake case, or explain how to expand this behavior yourself

Try this:

<script>
    $(document).ready(function () {
        "use strict";

        // Ensure that the PresetEngine exists
        if (!$.oc || !$.oc.presetEngine) {
            console.error('PresetEngine not found. Make sure input.presetengine.js is loaded.');
            return;
        }

        // Override the formatValue method
        var originalFormatValue = $.oc.presetEngine.formatValue;

        $.oc.presetEngine.formatValue = function(options, srcValue) {
            if (typeof srcValue === 'undefined') {
                return '';
            }

            if (options.inputPresetType === 'snake') {
                // Implement the snake case conversion directly
                return srcValue
                    .toLowerCase()
                    .replace(/[^\w\s]/g, '')  // Remove non-word characters
                    .replace(/\s+/g, '_');    // Replace spaces with underscores
            }

            // Call the original formatValue method for other types
            return originalFormatValue.call(this, options, srcValue);
        };

    });
</script>
2 Likes