UNPKG

nodejs-polars

Version:

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

126 lines (125 loc) 4.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SeriesStringFunctions = void 0; const _1 = require("."); const functions_1 = require("../lazy/functions"); const utils_1 = require("../utils"); const SeriesStringFunctions = (_s) => { const wrap = (method, ...args) => { const ret = _s[method](...args); return (0, _1._Series)(ret); }; 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 (0, _1._Series)(_s) .toFrame() .select((0, functions_1.col)(_s.name).str.concat(delimiter, ignoreNulls).as(_s.name)) .getColumn(_s.name); }, contains(pat) { return wrap("strContains", (0, utils_1.regexToString)(pat), false); }, 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'"); } }, extract(pat, groupIndex) { const s = (0, _1._Series)(_s); return s .toFrame() .select((0, functions_1.col)(s.name).str.extract(pat, groupIndex).as(s.name)) .getColumn(s.name); }, jsonDecode(dtype, inferSchemaLength) { return wrap("strJsonDecode", dtype, inferSchemaLength); }, jsonPathMatch(pat) { return wrap("strJsonPathMatch", [pat]); }, lengths() { return wrap("strLengths"); }, lstrip() { return wrap("strReplace", /^\s*/.source, ""); }, padStart(length, fillChar) { return wrap("strPadStart", length, fillChar); }, zFill(length) { return (0, _1._Series)(_s) .toFrame() .select((0, functions_1.col)(_s.name).str.zFill(length).as(_s.name)) .getColumn(_s.name); }, padEnd(length, fillChar) { return wrap("strPadEnd", length, fillChar); }, replace(pat, val) { return wrap("strReplace", (0, utils_1.regexToString)(pat), val); }, replaceAll(pat, val) { return wrap("strReplaceAll", (0, utils_1.regexToString)(pat), val); }, rstrip() { return wrap("strReplace", /[ \t]+$/.source, ""); }, slice(start, length) { const s = (0, _1._Series)(_s); return s .toFrame() .select((0, functions_1.col)(s.name).str.slice(start, length).as(s.name)) .getColumn(s.name); }, split(by, options) { const inclusive = typeof options === "boolean" ? options : options?.inclusive; const s = (0, _1._Series)(_s); return s .toFrame() .select((0, functions_1.col)(s.name).str.split(by, inclusive).as(s.name)) .getColumn(s.name); }, strip() { const s = (0, _1._Series)(_s); return s .toFrame() .select((0, functions_1.col)(s.name).str.strip().as(s.name)) .getColumn(s.name); }, strptime(dtype, fmt) { const s = (0, _1._Series)(_s); return s .toFrame() .select((0, functions_1.col)(s.name).str.strptime(dtype, fmt).as(s.name)) .getColumn(s.name); }, toLowerCase() { return wrap("strToLowercase"); }, toUpperCase() { return wrap("strToUppercase"); }, }; }; exports.SeriesStringFunctions = SeriesStringFunctions;