UNPKG

nodejs-polars

Version:

Polars: Blazingly fast DataFrames in Rust, Python, Node.js, R and SQL

132 lines (131 loc) 5.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExprStringFunctions = void 0; const datatypes_1 = require("../../datatypes"); const expr_1 = require("../expr"); const functions_1 = require("../functions"); const ExprStringFunctions = (_expr) => { const wrap = (method, ...args) => { return (0, expr_1._Expr)(_expr[method](...args)); }; const handleDecode = (encoding, strict) => { switch (encoding) { case "hex": return wrap("strHexDecode", strict); case "base64": return wrap("strBase64Decode", strict); default: throw new RangeError("supported encodings are 'hex' and 'base64'"); } }; return { concat(delimiter, ignoreNulls = true) { return wrap("strConcat", delimiter, ignoreNulls); }, contains(pat, literal = false, strict = true) { return wrap("strContains", (0, expr_1.exprToLitOrExpr)(pat)._expr, literal, strict); }, decode(arg, strict = false) { if (typeof arg === "string") { return handleDecode(arg, strict); } return handleDecode(arg.encoding, arg.strict); }, encode(encoding) { switch (encoding) { case "hex": return wrap("strHexEncode"); case "base64": return wrap("strBase64Encode"); default: throw new RangeError("supported encodings are 'hex' and 'base64'"); } }, endsWith(suffix) { return wrap("strEndsWith", (0, expr_1.exprToLitOrExpr)(suffix)._expr); }, extract(pattern, groupIndex) { return wrap("strExtract", (0, expr_1.exprToLitOrExpr)(pattern, true)._expr, groupIndex); }, jsonDecode(dtype) { if (!dtype) throw new Error("`Expr.str.json_decode` needs an explicitly given `dtype` otherwise Polars is not able to determine the output type. If you want to eagerly infer datatype you can use `Series.str.json_decode`."); return wrap("strJsonDecode", dtype); }, jsonPathMatch(pat) { return wrap("strJsonPathMatch", [pat]); }, lengths() { return wrap("strLengths"); }, lstrip() { return wrap("strLstrip"); }, replace(pat, val, literal = false, n = 1) { return wrap("strReplace", (0, expr_1.exprToLitOrExpr)(pat)._expr, (0, expr_1.exprToLitOrExpr)(val)._expr, literal, n); }, replaceAll(pat, val, literal = false) { return wrap("strReplaceAll", (0, expr_1.exprToLitOrExpr)(pat)._expr, (0, expr_1.exprToLitOrExpr)(val)._expr, literal); }, rstrip() { return wrap("strRstrip"); }, padStart(length, fillChar) { return wrap("strPadStart", (0, functions_1.lit)(length)._expr, fillChar); }, zFill(length) { if (!expr_1.Expr.isExpr(length)) { length = (0, functions_1.lit)(length)._expr; } return wrap("zfill", length); }, padEnd(length, fillChar) { return wrap("strPadEnd", (0, functions_1.lit)(length)._expr, fillChar); }, slice(start, length) { if (!expr_1.Expr.isExpr(start)) { start = (0, functions_1.lit)(start)._expr; } if (!expr_1.Expr.isExpr(length)) { length = (0, functions_1.lit)(length)._expr; } return wrap("strSlice", start, length); }, split(by, options) { const inclusive = typeof options === "boolean" ? options : options?.inclusive; return wrap("strSplit", (0, expr_1.exprToLitOrExpr)(by)._expr, inclusive); }, startsWith(prefix) { return wrap("strStartsWith", (0, expr_1.exprToLitOrExpr)(prefix)._expr); }, strip() { return wrap("strStrip"); }, stripChars(pattern) { return wrap("strStripChars", (0, expr_1.exprToLitOrExpr)(pattern)._expr, true, true); }, stripCharsEnd(pattern) { return wrap("strStripChars", (0, expr_1.exprToLitOrExpr)(pattern)._expr, false, true); }, stripCharsStart(pattern) { return wrap("strStripChars", (0, expr_1.exprToLitOrExpr)(pattern)._expr, true, false); }, strptime(dtype, format) { const dt = dtype instanceof datatypes_1.DataType ? dtype : dtype(); if (dt.equals(datatypes_1.DataType.Date)) { return wrap("strToDate", format, false, false, false); } if (dt.equals(datatypes_1.DataType.Datetime("ms"))) { return wrap("strToDatetime", format, undefined, undefined, false, false, false, undefined); } throw new Error(`only "DataType.Date" and "DataType.Datetime" are supported`); }, toLowerCase() { return wrap("strToLowercase"); }, toUpperCase() { return wrap("strToUppercase"); }, }; }; exports.ExprStringFunctions = ExprStringFunctions;