git.delta.rocks / jrsonnet / refs/commits / dbbcbc4becf1

difftreelog

source

crates/jrsonnet-stdlib/src/std.jsonnet7.7 KiBsourcehistory
1{2  local std = self,3  local id = std.id,45  thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',67  lstripChars(str, chars)::8    if std.length(str) > 0 && std.member(chars, str[0]) then9      std.lstripChars(str[1:], chars)10    else11      str,1213  rstripChars(str, chars)::14    local len = std.length(str);15    if len > 0 && std.member(chars, str[len - 1]) then16      std.rstripChars(str[:len - 1], chars)17    else18      str,1920  stripChars(str, chars)::21    std.lstripChars(std.rstripChars(str, chars), chars),2223  splitLimitR(str, c, maxsplits)::24    if maxsplits == -1 then25      std.splitLimit(str, c, -1)26    else27      local revStr(str) = std.join('', std.reverse(std.stringChars(str)));28      std.map(function(e) revStr(e), std.reverse(std.splitLimit(revStr(str), revStr(c), maxsplits))),2930  split(str, c):: std.splitLimit(str, c, -1),3132  mapWithIndex(func, arr)::33    if !std.isFunction(func) then34      error ('std.mapWithIndex first param must be function, got ' + std.type(func))35    else if !std.isArray(arr) && !std.isString(arr) then36      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))37    else38      std.makeArray(std.length(arr), function(i) func(i, arr[i])),3940  mapWithKey(func, obj)::41    if !std.isFunction(func) then42      error ('std.mapWithKey first param must be function, got ' + std.type(func))43    else if !std.isObject(obj) then44      error ('std.mapWithKey second param must be object, got ' + std.type(obj))45    else46      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },4748  lines(arr)::49    std.join('\n', arr + ['']),5051  deepJoin(arr)::52    if std.isString(arr) then53      arr54    else if std.isArray(arr) then55      std.join('', [std.deepJoin(x) for x in arr])56    else57      error 'Expected string or array, got %s' % std.type(arr),5859  assertEqual(a, b)::60    if a == b then61      true62    else63      error 'Assertion failed. ' + a + ' != ' + b,6465  clamp(x, minVal, maxVal)::66    if x < minVal then minVal67    else if x > maxVal then maxVal68    else x,6970  manifestIni(ini)::71    local body_lines(body) =72      std.join([], [73        local value_or_values = body[k];74        if std.isArray(value_or_values) then75          ['%s = %s' % [k, value] for value in value_or_values]76        else77          ['%s = %s' % [k, value_or_values]]7879        for k in std.objectFields(body)80      ]);8182    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),83          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],84          all_sections = [85      section_lines(k, ini.sections[k])86      for k in std.objectFields(ini.sections)87    ];88    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),8990  manifestToml(value):: std.manifestTomlEx(value, '  '),9192  escapeStringPython(str)::93    std.escapeStringJson(str),9495  escapeStringBash(str_)::96    local str = std.toString(str_);97    local trans(ch) =98      if ch == "'" then99        "'\"'\"'"100      else101        ch;102    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),103104  escapeStringDollars(str_)::105    local str = std.toString(str_);106    local trans(ch) =107      if ch == '$' then108        '$$'109      else110        ch;111    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),112113  local xml_escapes = {114    '<': '&lt;',115    '>': '&gt;',116    '&': '&amp;',117    '"': '&quot;',118    "'": '&apos;',119  },120121  escapeStringXML(str_)::122    local str = std.toString(str_);123    std.join('', [std.get(xml_escapes, ch, ch) for ch in std.stringChars(str)]),124125  manifestJson(value):: std.manifestJsonEx(value, '    ') tailstrict,126127  manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),128129  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true, quote_keys=true)::130    if !std.isArray(value) then131      error 'manifestYamlStream only takes arrays, got ' + std.type(value)132    else133      '---\n' + std.join(134        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object, quote_keys) for e in value]135      ) + if c_document_end then '\n...\n' else '\n',136137  manifestPython(v)::138    if std.isObject(v) then139      local fields = [140        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]141        for k in std.objectFields(v)142      ];143      '{%s}' % [std.join(', ', fields)]144    else if std.isArray(v) then145      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]146    else if std.isString(v) then147      '%s' % [std.escapeStringPython(v)]148    else if std.isFunction(v) then149      error 'cannot manifest function'150    else if std.isNumber(v) then151      std.toString(v)152    else if v == true then153      'True'154    else if v == false then155      'False'156    else if v == null then157      'None',158159  manifestPythonVars(conf)::160    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];161    std.join('\n', vars + ['']),162163  manifestXmlJsonml(value)::164    if !std.isArray(value) then165      error 'Expected a JSONML value (an array), got %s' % std.type(value)166    else167      local aux(v) =168        if std.isString(v) then169          v170        else171          local tag = v[0];172          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);173          local attrs = if has_attrs then v[1] else {};174          local children = if has_attrs then v[2:] else v[1:];175          local attrs_str =176            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);177          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);178179      aux(value),180181  mergePatch(target, patch)::182    if std.isObject(patch) then183      local target_object =184        if std.isObject(target) then target else {};185186      local target_fields =187        if std.isObject(target_object) then std.objectFields(target_object) else [];188189      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];190      local both_fields = std.setUnion(target_fields, std.objectFields(patch));191192      {193        [k]:194          if !std.objectHas(patch, k) then195            target_object[k]196          else if !std.objectHas(target_object, k) then197            std.mergePatch(null, patch[k]) tailstrict198          else199            std.mergePatch(target_object[k], patch[k]) tailstrict200        for k in std.setDiff(both_fields, null_fields)201      }202    else203      patch,204205  get(o, f, default=null, inc_hidden=true)::206    if std.objectHasEx(o, f, inc_hidden) then o[f] else default,207208  resolvePath(f, r)::209    local arr = std.split(f, '/');210    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),211212  prune(a)::213    local isContent(b) =214      if b == null then215        false216      else if std.isArray(b) then217        std.length(b) > 0218      else if std.isObject(b) then219        std.length(b) > 0220      else221        true;222    if std.isArray(a) then223      [std.prune(x) for x in a if isContent($.prune(x))]224    else if std.isObject(a) then {225      [x]: $.prune(a[x])226      for x in std.objectFields(a)227      if isContent(std.prune(a[x]))228    } else229      a,230231  find(value, arr)::232    if !std.isArray(arr) then233      error 'find second parameter should be an array, got ' + std.type(arr)234    else235      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),236237  // Compat238  __compare_array(arr1, arr2)::239    assert std.isArray(arr1) && std.isArray(arr2);240    std.__compare(arr1, arr2),241  __array_less(arr1, arr2):: std.__compare_array(arr1, arr2) == -1,242  __array_greater(arr1, arr2):: std.__compare_array(arr1, arr2) == 1,243  __array_less_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) <= 0,244  __array_greater_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) >= 0,245}