srcmod_enum.tengo 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. is_enumerable := func(x) {
  2. return is_array(x) || is_map(x) || is_immutable_array(x) || is_immutable_map(x)
  3. }
  4. is_array_like := func(x) {
  5. return is_array(x) || is_immutable_array(x)
  6. }
  7. export {
  8. // all returns true if the given function `fn` evaluates to a truthy value on
  9. // all of the items in `x`. It returns undefined if `x` is not enumerable.
  10. all: func(x, fn) {
  11. if !is_enumerable(x) { return undefined }
  12. for k, v in x {
  13. if !fn(k, v) { return false }
  14. }
  15. return true
  16. },
  17. // any returns true if the given function `fn` evaluates to a truthy value on
  18. // any of the items in `x`. It returns undefined if `x` is not enumerable.
  19. any: func(x, fn) {
  20. if !is_enumerable(x) { return undefined }
  21. for k, v in x {
  22. if fn(k, v) { return true }
  23. }
  24. return false
  25. },
  26. // chunk returns an array of elements split into groups the length of size.
  27. // If `x` can't be split evenly, the final chunk will be the remaining elements.
  28. // It returns undefined if `x` is not array.
  29. chunk: func(x, size) {
  30. if !is_array_like(x) || !size { return undefined }
  31. numElements := len(x)
  32. if !numElements { return [] }
  33. res := []
  34. idx := 0
  35. for idx < numElements {
  36. res = append(res, x[idx:idx+size])
  37. idx += size
  38. }
  39. return res
  40. },
  41. // at returns an element at the given index (if `x` is array) or
  42. // key (if `x` is map). It returns undefined if `x` is not enumerable.
  43. at: func(x, key) {
  44. if !is_enumerable(x) { return undefined }
  45. if is_array_like(x) {
  46. if !is_int(key) { return undefined }
  47. } else {
  48. if !is_string(key) { return undefined }
  49. }
  50. return x[key]
  51. },
  52. // each iterates over elements of `x` and invokes `fn` for each element. `fn` is
  53. // invoked with two arguments: `key` and `value`. `key` is an int index
  54. // if `x` is array. `key` is a string key if `x` is map. It does not iterate
  55. // and returns undefined if `x` is not enumerable.
  56. each: func(x, fn) {
  57. if !is_enumerable(x) { return undefined }
  58. for k, v in x {
  59. fn(k, v)
  60. }
  61. },
  62. // filter iterates over elements of `x`, returning an array of all elements `fn`
  63. // returns truthy for. `fn` is invoked with two arguments: `key` and `value`.
  64. // `key` is an int index if `x` is array. It returns undefined if `x` is not array.
  65. filter: func(x, fn) {
  66. if !is_array_like(x) { return undefined }
  67. dst := []
  68. for k, v in x {
  69. if fn(k, v) { dst = append(dst, v) }
  70. }
  71. return dst
  72. },
  73. // find iterates over elements of `x`, returning value of the first element `fn`
  74. // returns truthy for. `fn` is invoked with two arguments: `key` and `value`.
  75. // `key` is an int index if `x` is array. `key` is a string key if `x` is map.
  76. // It returns undefined if `x` is not enumerable.
  77. find: func(x, fn) {
  78. if !is_enumerable(x) { return undefined }
  79. for k, v in x {
  80. if fn(k, v) { return v }
  81. }
  82. },
  83. // find_key iterates over elements of `x`, returning key or index of the first
  84. // element `fn` returns truthy for. `fn` is invoked with two arguments: `key`
  85. // and `value`. `key` is an int index if `x` is array. `key` is a string key if
  86. // `x` is map. It returns undefined if `x` is not enumerable.
  87. find_key: func(x, fn) {
  88. if !is_enumerable(x) { return undefined }
  89. for k, v in x {
  90. if fn(k, v) { return k }
  91. }
  92. },
  93. // map creates an array of values by running each element in `x` through `fn`.
  94. // `fn` is invoked with two arguments: `key` and `value`. `key` is an int index
  95. // if `x` is array. `key` is a string key if `x` is map. It returns undefined
  96. // if `x` is not enumerable.
  97. map: func(x, fn) {
  98. if !is_enumerable(x) { return undefined }
  99. dst := []
  100. for k, v in x {
  101. dst = append(dst, fn(k, v))
  102. }
  103. return dst
  104. },
  105. // key returns the first argument.
  106. key: func(k, _) { return k },
  107. // value returns the second argument.
  108. value: func(_, v) { return v }
  109. }