-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
DefaultScripts.String.cs
376 lines (307 loc) · 19.3 KB
/
DefaultScripts.String.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ServiceStack.Script;
using ServiceStack.Text;
namespace ServiceStack.Script
{
// ReSharper disable InconsistentNaming
public partial class DefaultScripts
{
public IRawString raw(object value)
{
if (value == null || Equals(value, JsNull.Value))
return ScriptConstants.EmptyRawString;
if (value is string s)
return s == string.Empty ? ScriptConstants.EmptyRawString : s.ToRawString();
if (value is IRawString r)
return r;
if (value is bool b)
return b ? ScriptConstants.TrueRawString : ScriptConstants.FalseRawString;
var rawStr = value.ToString().ToRawString();
return rawStr;
}
public string appSetting(string name) => Context.AppSettings.GetString(name);
public string indent() => Context.Args[ScriptConstants.DefaultIndent] as string;
public string indents(int count) => repeat(Context.Args[ScriptConstants.DefaultIndent] as string, count);
public string space() => " ";
public string spaces(int count) => padLeft("", count, ' ');
public string newLine() => Context.Args[ScriptConstants.DefaultNewLine] as string;
public string newLines(int count) => repeat(newLine(), count);
public string newLine(string target) => target + newLine();
public string currency(decimal decimalValue) => currency(decimalValue, null); //required to support 1/2 vars
public string currency(decimal decimalValue, string culture)
{
var cultureInfo = culture != null
? new CultureInfo(culture)
: (CultureInfo)Context.Args[ScriptConstants.DefaultCulture];
var fmt = string.Format(cultureInfo, "{0:C}", decimalValue);
return fmt;
}
public IRawString formatRaw(object obj, string fmt) => raw(string.Format(fmt.Replace("{{","{").Replace("}}","}"), obj));
public string format(object obj, string format) => obj is IFormattable formattable
? formattable.ToString(format, null)
: string.Format(format, obj);
public string fmt(string format, object arg)
{
if (arg is object[] args)
return string.Format(format, args);
if (arg is List<object> argsList)
return string.Format(format, argsList.ToArray());
return string.Format(format, arg);
}
public string fmt(string format, object arg0, object arg1) => string.Format(format, arg0, arg1);
public string fmt(string format, object arg0, object arg1, object arg2) => string.Format(format, arg0, arg1, arg2);
public string append(string target, string suffix) => target + suffix;
public string appendLine(string target) => target + newLine();
public string appendFmt(string target, string format, object arg) => target + fmt(format, arg);
public string appendFmt(string target, string format, object arg0, object arg1) => target + fmt(format, arg0, arg1);
public string appendFmt(string target, string format, object arg0, object arg1, object arg2) => target + fmt(format, arg0, arg1, arg2);
public string dateFormat(DateTime dateValue) => dateValue.ToString((string)Context.Args[ScriptConstants.DefaultDateFormat]);
public string dateFormat(DateTime dateValue, string format) => dateValue.ToString(format ?? throw new ArgumentNullException(nameof(format)));
public string dateTimeFormat(DateTime dateValue) => dateValue.ToString((string)Context.Args[ScriptConstants.DefaultDateTimeFormat]);
public string timeFormat(TimeSpan timeValue) => timeValue.ToString((string)Context.Args[ScriptConstants.DefaultTimeFormat]);
public string timeFormat(TimeSpan timeValue, string format) => timeValue.ToString(format);
public string splitCase(string text) => text.SplitCamelCase().Replace('_', ' ').Replace(" ", " ");
public string humanize(string text) => splitCase(text).ToTitleCase();
public string titleCase(string text) => text.ToTitleCase();
public string pascalCase(string text) => text.ToPascalCase();
public string camelCase(string text) => text.ToCamelCase();
public string snakeCase(string text) => text.ToLowercaseUnderscore();
public string kebabCase(string text) => text.ToLowercaseUnderscore().Replace("_","-");
public string textStyle(string text, string headerStyle)
{
if (text == null) return null;
switch (headerStyle)
{
case nameof(splitCase):
return splitCase(text);
case nameof(humanize):
return humanize(text);
case nameof(titleCase):
return titleCase(text);
case nameof(pascalCase):
return pascalCase(text);
case nameof(camelCase):
return camelCase(text);
case nameof(snakeCase):
return snakeCase(text);
case nameof(kebabCase):
return kebabCase(text);
}
return text;
}
public string lower(string text) => text?.ToLower();
public string upper(string text) => text?.ToUpper();
public string substring(string text, int startIndex) => text.SafeSubstring(startIndex);
public string substring(string text, int startIndex, int length) => text.SafeSubstring(startIndex, length);
[Obsolete("typo")] public string substringWithElipsis(string text, int length) => text.SubstringWithEllipsis(0, length);
[Obsolete("typo")] public string substringWithElipsis(string text, int startIndex, int length) => text.SubstringWithEllipsis(startIndex, length);
public string substringWithEllipsis(string text, int length) => text.SubstringWithEllipsis(0, length);
public string substringWithEllipsis(string text, int startIndex, int length) => text.SubstringWithEllipsis(startIndex, length);
public string leftPart(string text, string needle) => text.LeftPart(needle);
public string rightPart(string text, string needle) => text.RightPart(needle);
public string lastLeftPart(string text, string needle) => text.LastLeftPart(needle);
public string lastRightPart(string text, string needle) => text.LastRightPart(needle);
public int compareTo(string text, string other) => string.Compare(text, other, (StringComparison)Context.Args[ScriptConstants.DefaultStringComparison]);
public bool startsWith(string text, string needle) => text?.StartsWith(needle) == true;
public bool endsWith(string text, string needle) => text?.EndsWith(needle) == true;
public string replace(string text, string oldValue, string newValue) => text.Replace(oldValue, newValue);
public string trimStart(string text) => text?.TrimStart();
public string trimStart(string text, char c) => text?.TrimStart(c);
public string trimEnd(string text) => text?.TrimEnd();
public string trimEnd(string text, char c) => text?.TrimEnd(c);
public string trim(string text) => text?.Trim();
public string trim(string text, char c) => text?.Trim(c);
public string padLeft(string text, int totalWidth) => text?.PadLeft(AssertWithinMaxQuota(totalWidth));
public string padLeft(string text, int totalWidth, char padChar) => text?.PadLeft(AssertWithinMaxQuota(totalWidth), padChar);
public string padRight(string text, int totalWidth) => text?.PadRight(AssertWithinMaxQuota(totalWidth));
public string padRight(string text, int totalWidth, char padChar) => text?.PadRight(AssertWithinMaxQuota(totalWidth), padChar);
public string[] splitOnFirst(string text, string needle) => text.SplitOnFirst(needle);
public string[] splitOnLast(string text, string needle) => text.SplitOnLast(needle);
public string[] split(string stringList) => stringList.Split(',');
public string[] split(string stringList, object delimiter)
{
if (delimiter is IEnumerable<object> objDelims)
delimiter = objDelims.Select(x => x.ToString());
if (delimiter is char c)
return stringList.Split(c);
if (delimiter is string s)
return s.Length == 1
? stringList.Split(s[0])
: stringList.Split(new[] { s }, StringSplitOptions.RemoveEmptyEntries);
if (delimiter is IEnumerable<string> strDelims)
return strDelims.All(x => x.Length == 1)
? stringList.Split(strDelims.Select(x => x[0]).ToArray(), StringSplitOptions.RemoveEmptyEntries)
: stringList.Split(strDelims.ToArray(), StringSplitOptions.RemoveEmptyEntries);
throw new NotSupportedException($"{delimiter} is not a valid delimiter");
}
public string[] glob(IEnumerable<string> strings, string pattern)
{
var list = strings?.ToList() ?? new List<string>();
return list.Where(x => x.Glob(pattern)).ToArray();
}
public string globln(IEnumerable<string> strings, string pattern) => joinln(glob(strings, pattern));
public Dictionary<string, string> parseKeyValueText(string target) => target?.ParseKeyValueText();
public Dictionary<string, string> parseKeyValueText(string target, string delimiter) => target?.ParseKeyValueText(delimiter);
public IEnumerable<KeyValuePair<string,string>> parseAsKeyValues(string target) => target?.ParseAsKeyValues();
public IEnumerable<KeyValuePair<string,string>> parseAsKeyValues(string target, string delimiter) => target?.ParseAsKeyValues(delimiter);
public ICollection keys(object target) =>
target is IDictionary d
? d.Keys
: target is IList l
? times(l.Count)
: throw new NotSupportedException($"{target.GetType().Name} is not supported");
public ICollection values(object target) =>
target is IDictionary d
? d.Values
: target is IList l
? l
: throw new NotSupportedException($"{target.GetType().Name} is not supported");
public string addPath(string target, string pathToAppend) => target.AppendPath(pathToAppend);
public string addPaths(string target, IEnumerable pathsToAppend) =>
target.AppendPath(pathsToAppend.Map(x => x.ToString()).ToArray());
public string addQueryString(string url, object urlParams) =>
urlParams.AssertOptions(nameof(addQueryString)).Aggregate(url, (current, entry) => current.AddQueryParam(entry.Key, entry.Value));
public string addHashParams(string url, object urlParams) =>
urlParams.AssertOptions(nameof(addHashParams)).Aggregate(url, (current, entry) => current.AddHashParam(entry.Key, entry.Value));
public string setQueryString(string url, object urlParams) =>
urlParams.AssertOptions(nameof(setQueryString)).Aggregate(url, (current, entry) => current.SetQueryParam(entry.Key, entry.Value?.ToString()));
public string setHashParams(string url, object urlParams) =>
urlParams.AssertOptions(nameof(setHashParams)).Aggregate(url, (current, entry) => current.SetHashParam(entry.Key, entry.Value?.ToString()));
public string repeating(int times, string text) => repeat(text, AssertWithinMaxQuota(times));
public string repeat(string text, int times)
{
AssertWithinMaxQuota(times);
var sb = StringBuilderCache.Allocate();
for (var i = 0; i < times; i++)
{
sb.Append(text);
}
return StringBuilderCache.ReturnAndFree(sb);
}
public string escapeSingleQuotes(string text) => text?.Replace("'", "\\'");
public string escapeDoubleQuotes(string text) => text?.Replace("\"", "\\\"");
public string escapeBackticks(string text) => text?.Replace("`", "\\`");
public string escapePrimeQuotes(string text) => text?.Replace("′", "\\′");
public string escapeNewLines(string text) => text?.Replace("\r", "\\r").Replace("\n", "\\n");
public IRawString jsString(string text) => string.IsNullOrEmpty(text)
? RawString.Empty
: escapeNewLines(escapeSingleQuotes(text)).ToRawString();
public IRawString jsQuotedString(string text) =>
("'" + escapeNewLines(escapeSingleQuotes(text ?? "")) + "'").ToRawString();
private async Task serialize(ScriptScopeContext scope, object items, string jsconfig, Func<object, string> fn)
{
var defaultJsConfig = Context.Args[ScriptConstants.DefaultJsConfig] as string;
jsconfig = jsconfig != null && !string.IsNullOrEmpty(defaultJsConfig)
? defaultJsConfig + "," + jsconfig
: defaultJsConfig;
if (jsconfig != null)
{
using (JsConfig.CreateScope(jsconfig))
{
await scope.OutputStream.WriteAsync(fn(items)).ConfigAwait();
return;
}
}
await scope.OutputStream.WriteAsync(items.ToJson()).ConfigAwait();
}
private IRawString serialize(object target, string jsconfig, Func<object, string> fn)
{
var defaultJsConfig = Context.Args[ScriptConstants.DefaultJsConfig] as string;
jsconfig = jsconfig != null && !string.IsNullOrEmpty(defaultJsConfig)
? defaultJsConfig + "," + jsconfig
: defaultJsConfig;
if (jsconfig == null)
return fn(target.AssertNoCircularDeps()).ToRawString();
using (JsConfig.CreateScope(jsconfig))
{
return fn(target).ToRawString();
}
}
static string jsonOrNull(object x)
{
if (x != null)
{
var json = x.ToJson();
if (!string.IsNullOrEmpty(json))
return json;
}
return "null";
}
//Filters
public IRawString json(object value) => serialize(value, null, jsonOrNull);
public IRawString json(object value, string jsconfig) => serialize(value, jsconfig, jsonOrNull);
public IRawString jsv(object value) => serialize(value, null, x => x.ToJsv() ?? "");
public IRawString jsv(object value, string jsconfig) => serialize(value, jsconfig, x => x.ToJsv() ?? "");
public IRawString csv(object value) => (value.AssertNoCircularDeps().ToCsv() ?? "").ToRawString();
public IRawString dump(object value) => serialize(value, null, x => x.Dump() ?? "");
public IRawString indentJson(object value) => indentJson(value, null);
public IRawString indentJson(object value, string jsconfig) =>
(value is string js ? js : json(value).ToRawString()).IndentJson().ToRawString();
//Blocks
public Task json(ScriptScopeContext scope, object items) => json(scope, items, null);
public Task json(ScriptScopeContext scope, object items, string jsConfig) => serialize(scope, items, jsConfig, x => x.ToJson());
public Task jsv(ScriptScopeContext scope, object items) => jsv(scope, items, null);
public Task jsv(ScriptScopeContext scope, object items, string jsConfig) => serialize(scope, items, jsConfig, x => x.ToJsv());
public Task dump(ScriptScopeContext scope, object items) => jsv(scope, items, null);
public Task dump(ScriptScopeContext scope, object items, string jsConfig) => serialize(scope, items, jsConfig, x => x.Dump());
public Task csv(ScriptScopeContext scope, object items) => scope.OutputStream.WriteAsync(items.ToCsv());
public Task xml(ScriptScopeContext scope, object items) => scope.OutputStream.WriteAsync(items.ToXml());
public JsonObject jsonToObject(string json) => JsonObject.Parse(json);
public JsonArrayObjects jsonToArrayObjects(string json) => JsonArrayObjects.Parse(json);
public Dictionary<string, object> jsonToObjectDictionary(string json) => json.FromJson<Dictionary<string, object>>();
public Dictionary<string, string> jsonToStringDictionary(string json) => json.FromJson<Dictionary<string, string>>();
public Dictionary<string, object> jsvToObjectDictionary(string json) => json.FromJsv<Dictionary<string, object>>();
public Dictionary<string, string> jsvToStringDictionary(string json) => json.FromJsv<Dictionary<string, string>>();
public object eval(ScriptScopeContext scope, string js) => JS.eval(js, scope);
public object parseJson(string json) => JSON.parse(json);
public string base64(byte[] bytes) => Convert.ToBase64String(bytes);
public byte[] fromBase64(string base64) => Convert.FromBase64String(base64);
public List<List<string>> parseCsv(string csv)
{
var trimmedBody = StringBuilderCache.Allocate();
foreach (var line in csv.ReadLines())
{
trimmedBody.AppendLine(line.Trim());
}
var strList = trimmedBody.ToString().FromCsv<List<List<string>>>();
return strList;
}
public List<KeyValuePair<string, string>> parseKeyValues(string keyValuesText) =>
parseKeyValues(keyValuesText, " ");
public List<KeyValuePair<string, string>> parseKeyValues(string keyValuesText, string delimiter) =>
keyValuesText.Trim().ParseAsKeyValues(delimiter);
private static readonly Regex InvalidCharsRegex = new Regex(@"[^a-z0-9\s-]", RegexOptions.Compiled);
private static readonly Regex SpacesRegex = new Regex(@"\s", RegexOptions.Compiled);
private static readonly Regex CollapseHyphensRegex = new Regex("-+", RegexOptions.Compiled);
public string generateSlug(string phrase)
{
var str = phrase.ToLower()
.Replace("#", "sharp") // c#, f# => csharp, fsharp
.Replace("++", "pp"); // c++ => cpp
str = InvalidCharsRegex.Replace(str, "-");
//// convert multiple spaces into one space
//str = CollapseSpacesRegex.Replace(str, " ").Trim();
// cut and trim
str = str.Substring(0, str.Length <= 100 ? str.Length : 100).Trim();
str = SpacesRegex.Replace(str, "-");
str = CollapseHyphensRegex.Replace(str, "-");
if (string.IsNullOrEmpty(str))
return null;
if (str[0] == '-')
str = str.Substring(1);
if (str[str.Length - 1] == '-')
str = str.Substring(0, str.Length - 1);
return str;
}
public bool isBinary(string fileOrExt) => MimeTypes.IsBinary(MimeTypes.GetMimeType(fileOrExt));
public string contentType(string fileOrExt) => MimeTypes.GetMimeType(fileOrExt);
public static string[] splitLines(string contents) => contents.Replace("\r", "").Split('\n');
public IEnumerable<string> readLines(string contents) => contents.ReadLines();
}
}