Velocity - ScanReplace for non-printable characters

Version 1

    Replace any text or ASCII character in a barcode with another piece of text or ASCII character value. You can also replace a piece of text with empty text.

     

    Note: All instances of the given string will be replaced.

     

     

    Hexvalues:

    ScanHandler: removing a GS, FS, RS from a barcode

     

    the Script:

    //Let's create a function to process the scan

    function onScan(event)

    {

        Logger.debug("Scan - Replace - Script Beginning: Event = " + JSON.stringify(event) + " | Arguments = " + minLength + ", " + maxLength + ", " + allowedType + ", " + textToReplace + ", " + replacementText );

     

        //Make sure we meet the min and max length requirement. We do allow maxLength to be 0 which means no max length

        if( event.data.length < minLength || (maxLength != 0 && event.data.length > maxLength) )

            return;

     

        //Sometimes the scanner sends through a symbology with an "_" or spaces on the end. Lets make sure we fix this.

        var type = event.type.replace("_", "").trim();

     

        //Verify the symbology if it is not "ALL" and does not match then we need to return

        if( allowedType !== "ALL" && allowedType !== type )

            return;

     

        // check if textToReplace is an ASCII code and not and ASCII character, this is signalled by an # at the beginning

        if( textToReplace.charAt(0) == '#')

        {

            // strip the # character and use the value after that as the ASCII character to replace

            var ASCIIcode = textToReplace.split("#").pop();

     

            for( var i = 0; i < event.data.length; i++ )

            {

                if( event.data.charCodeAt(i) == parseInt(ASCIIcode) )

                {

                    // Check if the repleacement text is entered as an ASCII value or as and ASCII character

                    if( replacementText.charAt(0) == '#')

                    {

                        var charCode = parseInt(replacementText.split("#").pop());

                        event.data = event.data.substr(0, i) + String.fromCharCode(charCode) + event.data.substr(i + 1);

                    }

                    else

                    {

                        event.data = event.data.substr(0, i) + replacementText + event.data.substr(i + 1);

                    }

                }

            }

        }

        // Not an ASCII code, do normal replacement

        else

        {

            //Create a regular expression by escaping the string

            var regex = new RegExp( textToReplace.replace(  /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&" ), "g" );

     

            //This is where we modify the scan data

            event.data = event.data.replace( regex, replacementText );

        }

     

        Logger.debug("Scan - Replace - Script Ending: Event = " + JSON.stringify(event)  );

    }

     

    //Register the onScan function to be called when a scan occurs. Without this it will never run.

    WLEvent.on("Scan", onScan);