-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathcleanResult.js
More file actions
23 lines (21 loc) · 1.09 KB
/
cleanResult.js
File metadata and controls
23 lines (21 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
@name $SP().cleanResult
@function
@category lists
@description clean a string returned by a GET (remove ";#" and "string;#" and null becomes "")
@param {String} str The string to clean
@param {String} [separator=";"] When it's a list we may want to have a different output (see examples)
@return {String} the cleaned string
@example
$SP().cleanResult("15;#Paul"); // -> "Paul"
$SP().cleanResult("string;#Paul"); // -> "Paul"
$SP().cleanResult("string;#"); // -> ""
$SP().cleanResult(";#Paul;#Jacques;#Aymeric;#"); // -> "Paul;Jacques;Aymeric"
$SP().cleanResult(";#Paul;#Jacques;#Aymeric;#", ", "); // -> "Paul, Jacques, Aymeric"
$SP().cleanResult("2022-01-19 00:00:00"); // -> "2022-01-19"
*/
export default function cleanResult(str,separator) {
if (str===null || typeof str==="undefined") return "";
separator = separator || ";";
return (typeof str==="string"?str.replace(/^(string;|float;|datetime;)#?/,"").replace(/^(\d{4}-\d{2}-\d{2}) 00:00:00$/, "$1").replace(/;#-?[0-9]+;#/g,separator).replace(/^-?[0-9]+;#/,"").replace(/^;#|;#$/g,"").replace(/;#/g,separator):str);
}