Beispiel #1
0
func (t *TruncateWordsFilter) Truncate(input interface{}, data map[string]interface{}) interface{} {
	length, ok := core.ToInt(t.limit.Resolve(data))
	if ok == false {
		return input
	}

	var value string
	switch typed := input.(type) {
	case string:
		value = typed
	default:
		value = core.ToString(typed)
	}

	i := 2
	found := 0
	l := len(value)
	for ; i < l; i++ {
		if value[i] == ' ' && value[i-1] != ' ' && value[i-2] != ' ' {
			found++
			if found == length {
				break
			}
		}
	}

	if i == l {
		return input
	}
	return value[:i] + core.ToString(t.append.Resolve(data))
}
Beispiel #2
0
func (d *DateFilter) ToString(input interface{}, data map[string]interface{}) interface{} {
	time, ok := inputToTime(input)
	if ok == false {
		return input
	}
	return formatTime(time, core.ToString(d.format.Resolve(data)))
}
Beispiel #3
0
// splits a value on the given value and returns an array
func (f *SplitFilter) Split(input interface{}, data map[string]interface{}) interface{} {
	on := core.ToString(f.on.Resolve(data))
	switch typed := input.(type) {
	case string:
		return strings.Split(typed, on)
	case []byte:
		return strings.Split(string(typed), on)
	default:
		return input
	}
}
Beispiel #4
0
func (a *PrependFilter) Prepend(input interface{}, data map[string]interface{}) interface{} {
	var value string
	switch typed := a.value.Resolve(data).(type) {
	case string:
		value = typed
	default:
		value = string(core.ToBytes(typed))
	}
	switch typed := input.(type) {
	case string:
		return value + typed
	default:
		return value + core.ToString(typed)
	}
}
Beispiel #5
0
func (t *TruncateFilter) Truncate(input interface{}, data map[string]interface{}) interface{} {
	length, ok := core.ToInt(t.limit.Resolve(data))
	if ok == false {
		return input
	}

	var value string
	switch typed := input.(type) {
	case string:
		value = typed
	default:
		value = core.ToString(typed)
	}

	append := core.ToString(t.append.Resolve(data))
	length -= len(append)
	if length >= len(value) {
		return input
	}
	if length < 0 {
		return append
	}
	return value[:length] + append
}
Beispiel #6
0
// Sorts an array
func Sort(input interface{}, data map[string]interface{}) interface{} {
	switch typed := input.(type) {
	case []int:
		sort.Ints(typed)
		return typed
	case []string:
		sort.Strings(typed)
		return typed
	case []float64:
		sort.Float64s(typed)
		return typed
	case sort.Interface:
		sort.Sort(typed)
		return typed
	}

	value := reflect.ValueOf(input)
	kind := value.Kind()
	if kind != reflect.Array && kind != reflect.Slice {
		return input
	}
	l := value.Len()
	if l == 0 {
		return input
	}

	sortable := make(SortableObjects, l)
	for i := 0; i < l; i++ {
		value := value.Index(i).Interface()
		sortable[i] = &SortableObject{value, core.ToString(value)}
	}
	sort.Sort(sortable)

	sorted := make([]interface{}, l)
	for i := 0; i < l; i++ {
		sorted[i] = sortable[i].Underlying
	}
	return sorted
}
Beispiel #7
0
func (i *Include) Render(writer io.Writer, data map[string]interface{}) {
	if i.handler != nil {
		i.handler(core.ToString(i.value.Resolve(data)), writer, data)
	}
}
Beispiel #8
0
func (i *Include) Execute(writer io.Writer, data map[string]interface{}) core.ExecuteState {
	if i.handler != nil {
		i.handler(core.ToString(i.value.Resolve(data)), writer, data)
	}
	return core.Normal
}