aboutsummaryrefslogtreecommitdiffstats
path: root/alv-lib/string.moon
blob: e4b007ca983209ab2a86921814ddf718df1321e5 (plain)
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
import PureOp, Constant, Input, T, Array, any from require 'alv.base'

str = Constant.meta
  meta:
    name: 'str'
    summary: "Concatenate/stringify values."
    examples: { '(.. v1 [v2…])', '(str v1 [v2…])' }
  value: class extends PureOp
    pattern: any!\rep 1, nil
    type: T.str
    tick: =>
      strings = [i\type!\pp i!, true for i in *@inputs]
      @out\set table.concat strings

join = Constant.meta
  meta:
    name: 'join'
    summary: "Concatenate/stringify values (with separator)"
    examples: { '(join separator v1 [v2…])' }
  value: class extends PureOp
    pattern: any.str + any!\rep 1, nil
    type: T.str
    tick: =>
      strings = [i\type!\pp i!, true for i in *@inputs[2]]
      @out\set table.concat strings, @inputs[1]!

str_arr = any ((typ) -> typ.__class == Array and typ.type == T.str), "str[]"
concat = Constant.meta
  meta:
    name: 'concat'
    summary: "Concatenate string arrays."
    examples: { '(concat [separator] parts)' }
  value: class extends PureOp
    pattern: -any.str + str_arr
    type: T.str

    tick: =>
      { separator, parts } = @unwrap_all!
      @out\set table.concat parts, separator

Constant.meta
  meta:
    name: 'string'
    summary: "Utilities for dealing with strings."

  value:
    :str, '..': str
    :join
    :concat