create package body standard is
    function abs(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        return js.Math.abs(n);
    end;

    function ceil(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        return js.Math.ceil(n);
    end;

    function floor(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        return js.Math.floor(n);
    end;

    function round(n number, places number) return number is
        factor number;
        scaled number;
    begin
        if n is null then
            return null;
        end if;
        if places is null then
            places := 0;
        end if;
        factor := js.Math.pow(10, places);
        scaled := n * factor;
        if scaled >= 0 then
            return js.Math.floor(scaled + 0.5) / factor;
        end if;
        return js.Math.ceil(scaled - 0.5) / factor;
    end;

    function trunc(n number, places number) return number is
        factor number;
    begin
        if n is null then
            return null;
        end if;
        if places is null then
            places := 0;
        end if;
        factor := js.Math.pow(10, places);
        return js.Math.trunc(n * factor) / factor;
    end;

    function mod(n number, m number) return number is
    begin
        if n is null or m is null then
            return null;
        end if;
        if m = 0 then
            return n;
        end if;
        return n - m * js.Math.floor(n / m);
    end;

    function power(n number, m number) return number is
    begin
        if n is null or m is null then
            return null;
        end if;
        if n = 0 and m < 0 then
            return null;
        end if;
        if n < 0 and js.Math.floor(m) != m then
            return null;
        end if;
        return js.Math.pow(n, m);
    end;

    function sqrt(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        if n < 0 then
            return null;
        end if;
        return js.Math.sqrt(n);
    end;

    function ln(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        if n <= 0 then
            return null;
        end if;
        return js.Math.log(n);
    end;

    function log(base number, n number) return number is
    begin
        if base is null or n is null then
            return null;
        end if;
        if base <= 0 or base = 1 or n <= 0 then
            return null;
        end if;
        return js.Math.log(n) / js.Math.log(base);
    end;

    function exp(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        return js.Math.exp(n);
    end;

    function sign(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        if n > 0 then
            return 1;
        elsif n < 0 then
            return -1;
        end if;
        return 0;
    end;

    function sin(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        return js.Math.sin(n);
    end;

    function cos(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        return js.Math.cos(n);
    end;

    function tan(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        return js.Math.tan(n);
    end;

    function asin(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        if n < -1 or n > 1 then
            return null;
        end if;
        return js.Math.asin(n);
    end;

    function acos(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        if n < -1 or n > 1 then
            return null;
        end if;
        return js.Math.acos(n);
    end;

    function atan(n number) return number is
    begin
        if n is null then
            return null;
        end if;
        return js.Math.atan(n);
    end;

    function atan2(y number, x number) return number is
    begin
        if y is null or x is null then
            return null;
        end if;
        return js.Math.atan2(y, x);
    end;

    function least(a number, b number) return number is
        args js.Array := js.Array.from(arguments);
        result number;
        found boolean := false;
        i number := 0;
    begin
        while i < args.length loop
            if args.at(i) is not null then
                if not found then
                    result := args.at(i);
                    found := true;
                elsif args.at(i) < result then
                    result := args.at(i);
                end if;
            end if;
            i := i + 1;
        end loop;
        if not found then
            return null;
        end if;
        return result;
    end;

    function greatest(a number, b number) return number is
        args js.Array := js.Array.from(arguments);
        result number;
        found boolean := false;
        i number := 0;
    begin
        while i < args.length loop
            if args.at(i) is not null then
                if not found then
                    result := args.at(i);
                    found := true;
                elsif args.at(i) > result then
                    result := args.at(i);
                end if;
            end if;
            i := i + 1;
        end loop;
        if not found then
            return null;
        end if;
        return result;
    end;

    function length(s string) return number is
    begin
        if s is null then
            return null;
        end if;
        return s.length;
    end;

    function substr(s string, start_pos number, len number) return string is
        js_start number;
    begin
        if s is null or start_pos is null then
            return null;
        end if;
        if start_pos > 0 then
            js_start := start_pos - 1;
        elsif start_pos = 0 then
            js_start := 0;
        else
            js_start := s.length + start_pos;
        end if;
        if js_start < 0 or js_start >= s.length then
            return null;
        end if;
        if len is null then
            return s.substring(js_start);
        end if;
        if len <= 0 then
            return null;
        end if;
        return s.substring(js_start, js_start + len);
    end;

    function instr(s string, search string, start_pos number) return number is
        from_pos number;
        idx number;
    begin
        if s is null or search is null then
            return null;
        end if;
        if start_pos is null then
            start_pos := 1;
        end if;
        from_pos := start_pos - 1;
        if from_pos < 0 then
            from_pos := 0;
        end if;
        idx := s.indexOf(search, from_pos);
        if idx < 0 then
            return 0;
        end if;
        return idx + 1;
    end;

    function upper(s string) return string is
    begin
        if s is null then
            return null;
        end if;
        return s.toUpperCase();
    end;

    function lower(s string) return string is
    begin
        if s is null then
            return null;
        end if;
        return s.toLowerCase();
    end;

    function initcap(s string) return string is
        result string := '';
        ch string;
        lower_ch string;
        upper_ch string;
        prev_sep boolean := true;
        is_letter boolean;
        is_digit boolean;
    begin
        if s is null then
            return null;
        end if;
        for i in 0 .. s.length - 1 loop
            ch := s.charAt(i);
            lower_ch := ch.toLowerCase();
            upper_ch := ch.toUpperCase();
            is_letter := lower_ch != upper_ch;
            is_digit := ch >= '0' and ch <= '9';
            if is_letter or is_digit then
                if prev_sep then
                    result := result || upper_ch;
                else
                    result := result || lower_ch;
                end if;
                prev_sep := false;
            else
                result := result || ch;
                prev_sep := true;
            end if;
        end loop;
        return result;
    end;

    function trim(s string) return string is
        start_idx number := 0;
        end_idx number;
    begin
        if s is null then
            return null;
        end if;
        end_idx := s.length - 1;
        while start_idx <= end_idx and s.charAt(start_idx) = ' ' loop
            start_idx := start_idx + 1;
        end loop;
        while end_idx >= start_idx and s.charAt(end_idx) = ' ' loop
            end_idx := end_idx - 1;
        end loop;
        if start_idx > end_idx then
            return '';
        end if;
        return s.substring(start_idx, end_idx + 1);
    end;

    function ltrim(s string, chars string) return string is
        start_idx number := 0;
    begin
        if s is null then
            return null;
        end if;
        if chars is null then
            chars := ' ';
        end if;
        if chars = '' then
            return s;
        end if;
        while start_idx < s.length and chars.indexOf(s.charAt(start_idx)) >= 0 loop
            start_idx := start_idx + 1;
        end loop;
        return s.substring(start_idx);
    end;

    function rtrim(s string, chars string) return string is
        end_idx number;
    begin
        if s is null then
            return null;
        end if;
        if chars is null then
            chars := ' ';
        end if;
        if chars = '' then
            return s;
        end if;
        end_idx := s.length - 1;
        while end_idx >= 0 and chars.indexOf(s.charAt(end_idx)) >= 0 loop
            end_idx := end_idx - 1;
        end loop;
        if end_idx < 0 then
            return '';
        end if;
        return s.substring(0, end_idx + 1);
    end;

    function lpad(s string, len number, pad string) return string is
        pad_len number;
        padding string := '';
    begin
        if s is null or len is null then
            return null;
        end if;
        if pad is null or pad = '' then
            pad := ' ';
        end if;
        if len <= 0 then
            return '';
        end if;
        if s.length >= len then
            return s.substring(0, len);
        end if;
        pad_len := len - s.length;
        while padding.length < pad_len loop
            padding := padding || pad;
        end loop;
        padding := padding.substring(0, pad_len);
        return padding || s;
    end;

    function rpad(s string, len number, pad string) return string is
        pad_len number;
        padding string := '';
    begin
        if s is null or len is null then
            return null;
        end if;
        if pad is null or pad = '' then
            pad := ' ';
        end if;
        if len <= 0 then
            return '';
        end if;
        if s.length >= len then
            return s.substring(0, len);
        end if;
        pad_len := len - s.length;
        while padding.length < pad_len loop
            padding := padding || pad;
        end loop;
        padding := padding.substring(0, pad_len);
        return s || padding;
    end;

    function replace(s string, search string, replacement string) return string is
    begin
        if s is null then
            return null;
        end if;
        if search is null or search = '' then
            return s;
        end if;
        if replacement is null then
            replacement := '';
        end if;
        return s.split(search).join(replacement);
    end;

    function concat(a string, b string) return string is
    begin
        if a is null then
            a := '';
        end if;
        if b is null then
            b := '';
        end if;
        if a = '' and b = '' then
            return null;
        end if;
        return js.String(a) + js.String(b);
    end;

    function ascii(s string) return number is
    begin
        if s is null or s = '' then
            return null;
        end if;
        return s.charCodeAt(0);
    end;

    function chr(n number) return string is
    begin
        if n is null then
            return null;
        end if;
        return js.String.fromCharCode(n);
    end;

    function nvl(value string, fallback string) return string is
    begin
        if value is null then
            return fallback;
        end if;
        return value;
    end;

    function nvl2(value string, not_null_value string, null_value string) return string is
    begin
        if value is null then
            return null_value;
        end if;
        return not_null_value;
    end;

    function coalesce(a string, b string) return string is
        args js.Array := js.Array.from(arguments);
        i number := 0;
    begin
        while i < args.length loop
            if args.at(i) is not null then
                return args.at(i);
            end if;
            i := i + 1;
        end loop;
        return null;
    end;

    function nullif(a string, b string) return string is
    begin
        if a = b then
            return null;
        end if;
        return a;
    end;

    function decode(expr string, search string, result string, default_value string) return string is
        args js.Array := js.Array.from(arguments);
        i number := 1;
    begin
        while i + 1 < args.length loop
            if args.at(i) = expr then
                return args.at(i + 1);
            end if;
            i := i + 2;
        end loop;
        if i < args.length then
            return args.at(i);
        end if;
        return null;
    end;

    function to_char(value number) return string is
    begin
        if value is null then
            return null;
        end if;
        return js.String(value);
    end;

    function to_number(value string) return number is
        number_value number;
    begin
        if value is null then
            return null;
        end if;
        if js.String(value).trim() = '' then
            return null;
        end if;
        number_value := js.Number(value);
        if js.Number.isNaN(number_value) then
            return null;
        end if;
        return number_value;
    end;

end;
