penguin/golifehk

@golifehk bot in Telegram

datasources/cjlookup/CChar.go

raw ยท 1303 bytes

package cjlookup

import (
	query "github.com/tgckpg/golifehk/query"
	"golang.org/x/exp/slices"
	"strconv"
)

type CChar struct {
	Face      string
	CangJie   string
	JyutPing  *CJyutPing
	DukJam    *[]*DukJam
	_JiDukJam *[]*CJyutPing
	query.Words
	query.NoGeoLocation
}

type DukJam struct {
	Weight   int
	JyutPing *CJyutPing
}

type CJyutPing struct {
	Roman     string
	SearchKey string // Searchable key
	TungJamZi *[]*CChar
	tSorted   bool
	query.Words
	query.NoGeoLocation
}

func (this *CJyutPing) Test(val string) bool {
	_, err := strconv.Atoi(val)
	if err == nil {
		return true
	}
	return false
}

func (this *CChar) Test(val string) bool {
	_, err := strconv.Atoi(val)
	if err == nil {
		return true
	}
	return false
}

func (this *CChar) JiDukJam() *[]*CJyutPing {
	if this._JiDukJam == nil {
		jdj := &[]*CJyutPing{}
		for _, dj := range *this.DukJam {
			if dj.JyutPing != this.JyutPing {
				*jdj = append(*jdj, dj.JyutPing)
			}
		}
		this._JiDukJam = jdj
	}

	return this._JiDukJam
}

func (this *CJyutPing) SortedTungJamZi() *[]*CChar {
	if this.tSorted {
		return this.TungJamZi
	}

	slices.SortFunc(*this.TungJamZi, func(a, b *CChar) int {
		switch {
		case a.Face < b.Face:
			return -1
		case a.Face > b.Face:
			return 1
		default:
			return 0
		}
	})

	return this.TungJamZi
}