gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/SimpleSlice.go (about)

     1  package corestr
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"sort"
     8  	"strings"
     9  
    10  	"gitlab.com/evatix-go/core/constants"
    11  	"gitlab.com/evatix-go/core/coredata/corejson"
    12  )
    13  
    14  type SimpleSlice struct {
    15  	Items []string `json:"Items,omitempty"`
    16  }
    17  
    18  func (it *SimpleSlice) Add(
    19  	item string,
    20  ) *SimpleSlice {
    21  	it.Items = append(it.Items, item)
    22  
    23  	return it
    24  }
    25  
    26  func (it *SimpleSlice) AddIf(
    27  	isAdd bool,
    28  	item string,
    29  ) *SimpleSlice {
    30  	if !isAdd {
    31  		return it
    32  	}
    33  
    34  	it.Items = append(it.Items, item)
    35  
    36  	return it
    37  }
    38  
    39  func (it *SimpleSlice) Adds(
    40  	items ...string,
    41  ) *SimpleSlice {
    42  	if len(items) == 0 {
    43  		return it
    44  	}
    45  
    46  	it.Items = append(it.Items, items...)
    47  
    48  	return it
    49  }
    50  
    51  func (it *SimpleSlice) Append(
    52  	items ...string,
    53  ) *SimpleSlice {
    54  	if len(items) == 0 {
    55  		return it
    56  	}
    57  
    58  	it.Items = append(it.Items, items...)
    59  
    60  	return it
    61  }
    62  
    63  // AppendFmt
    64  //
    65  //  Skips empty format + values
    66  func (it *SimpleSlice) AppendFmt(
    67  	format string,
    68  	v ...interface{},
    69  ) *SimpleSlice {
    70  	if format == "" && len(v) == 0 {
    71  		return it
    72  	}
    73  
    74  	it.Items = append(
    75  		it.Items,
    76  		fmt.Sprintf(format, v...))
    77  
    78  	return it
    79  }
    80  
    81  // AppendFmtIf
    82  //
    83  //  Skips empty format + values
    84  func (it *SimpleSlice) AppendFmtIf(
    85  	isAppend bool,
    86  	format string,
    87  	v ...interface{},
    88  ) *SimpleSlice {
    89  	if !isAppend || format == "" && len(v) == 0 {
    90  		return it
    91  	}
    92  
    93  	it.Items = append(
    94  		it.Items,
    95  		fmt.Sprintf(format, v...))
    96  
    97  	return it
    98  }
    99  
   100  // AddAsTitleValue
   101  //
   102  //  Adds Title : value (constants.TitleValueFormat)
   103  func (it *SimpleSlice) AddAsTitleValue(
   104  	title string,
   105  	value interface{},
   106  ) *SimpleSlice {
   107  	it.Items = append(
   108  		it.Items,
   109  		fmt.Sprintf(constants.TitleValueFormat, title, value))
   110  
   111  	return it
   112  }
   113  
   114  // AddAsCurlyTitleWrap
   115  //
   116  //  Adds Title: {value} (constants.CurlyTitleWrapFormat)
   117  func (it *SimpleSlice) AddAsCurlyTitleWrap(
   118  	title string,
   119  	value interface{},
   120  ) *SimpleSlice {
   121  	it.Items = append(
   122  		it.Items,
   123  		fmt.Sprintf(constants.CurlyTitleWrapFormat, title, value))
   124  
   125  	return it
   126  }
   127  
   128  // AddAsCurlyTitleWrapIf
   129  //
   130  //  Adds Title: {value} (constants.CurlyTitleWrapFormat)
   131  func (it *SimpleSlice) AddAsCurlyTitleWrapIf(
   132  	isAppend bool,
   133  	title string,
   134  	value interface{},
   135  ) *SimpleSlice {
   136  	if !isAppend {
   137  		return it
   138  	}
   139  
   140  	it.Items = append(
   141  		it.Items,
   142  		fmt.Sprintf(constants.CurlyTitleWrapFormat, title, value))
   143  
   144  	return it
   145  }
   146  
   147  // AddAsTitleValueIf
   148  //
   149  //  Skips if append is false
   150  //  Adds Title : value (constants.TitleValueFormat)
   151  func (it *SimpleSlice) AddAsTitleValueIf(
   152  	isAppend bool,
   153  	title string,
   154  	value interface{},
   155  ) *SimpleSlice {
   156  	if !isAppend {
   157  		return it
   158  	}
   159  
   160  	it.Items = append(
   161  		it.Items,
   162  		fmt.Sprintf(constants.TitleValueFormat, title, value))
   163  
   164  	return it
   165  }
   166  
   167  func (it *SimpleSlice) InsertAt(
   168  	index int,
   169  	item string,
   170  ) *SimpleSlice {
   171  	it.Items = append(it.Items[:index+1], it.Items[index:]...)
   172  	it.Items[index] = item
   173  
   174  	return it
   175  }
   176  
   177  func (it *SimpleSlice) AddStruct(
   178  	isIncludeFieldName bool,
   179  	anyStruct interface{},
   180  ) *SimpleSlice {
   181  	if anyStruct == nil {
   182  		return it
   183  	}
   184  
   185  	val := AnyToString(
   186  		isIncludeFieldName,
   187  		anyStruct)
   188  
   189  	return it.Add(val)
   190  }
   191  
   192  func (it *SimpleSlice) AddPointer(
   193  	isIncludeFieldName bool,
   194  	anyPtr interface{},
   195  ) *SimpleSlice {
   196  	if anyPtr == nil {
   197  		return it
   198  	}
   199  
   200  	val := AnyToString(
   201  		isIncludeFieldName,
   202  		anyPtr)
   203  
   204  	return it.Add(val)
   205  }
   206  
   207  func (it *SimpleSlice) AddsIf(
   208  	isAdd bool,
   209  	items ...string,
   210  ) *SimpleSlice {
   211  	if !isAdd {
   212  		return it
   213  	}
   214  
   215  	return it.Adds(items...)
   216  }
   217  
   218  func (it *SimpleSlice) AddError(err error) *SimpleSlice {
   219  	if err != nil {
   220  		return it.Add(err.Error())
   221  	}
   222  
   223  	return it
   224  }
   225  
   226  func (it *SimpleSlice) AsDefaultError() error {
   227  	return it.AsError(constants.NewLineUnix)
   228  }
   229  
   230  func (it *SimpleSlice) AsError(joiner string) error {
   231  	if it == nil || it.Length() == 0 {
   232  		return nil
   233  	}
   234  
   235  	errStr := strings.Join(
   236  		it.Items,
   237  		joiner)
   238  
   239  	return errors.New(errStr)
   240  }
   241  
   242  func (it *SimpleSlice) FirstDynamic() interface{} {
   243  	return it.Items[0]
   244  }
   245  
   246  func (it *SimpleSlice) First() string {
   247  	return it.Items[0]
   248  }
   249  
   250  func (it *SimpleSlice) LastDynamic() interface{} {
   251  	return it.Items[it.LastIndex()]
   252  }
   253  
   254  func (it *SimpleSlice) Last() string {
   255  	return it.Items[it.LastIndex()]
   256  }
   257  
   258  func (it *SimpleSlice) FirstOrDefaultDynamic() interface{} {
   259  	return it.FirstOrDefault()
   260  }
   261  
   262  func (it *SimpleSlice) FirstOrDefault() string {
   263  	if it.IsEmpty() {
   264  		return constants.EmptyString
   265  	}
   266  
   267  	return it.First()
   268  }
   269  
   270  func (it *SimpleSlice) LastOrDefaultDynamic() interface{} {
   271  	return it.LastOrDefault()
   272  }
   273  
   274  func (it *SimpleSlice) LastOrDefault() string {
   275  	if it.IsEmpty() {
   276  		return constants.EmptyString
   277  	}
   278  
   279  	return it.Last()
   280  }
   281  
   282  func (it *SimpleSlice) SkipDynamic(skippingItemsCount int) interface{} {
   283  	return it.Items[skippingItemsCount:]
   284  }
   285  
   286  func (it *SimpleSlice) Skip(skippingItemsCount int) []string {
   287  	return it.Items[skippingItemsCount:]
   288  }
   289  
   290  func (it *SimpleSlice) TakeDynamic(takeDynamicItems int) interface{} {
   291  	return it.Items[:takeDynamicItems]
   292  }
   293  
   294  func (it *SimpleSlice) Take(takeDynamicItems int) []string {
   295  	return it.Items[:takeDynamicItems]
   296  }
   297  
   298  func (it *SimpleSlice) LimitDynamic(limit int) interface{} {
   299  	return it.Take(limit)
   300  }
   301  
   302  func (it *SimpleSlice) Limit(limit int) []string {
   303  	return it.Take(limit)
   304  }
   305  
   306  func (it *SimpleSlice) Length() int {
   307  	if it == nil {
   308  		return 0
   309  	}
   310  
   311  	return len(it.Items)
   312  }
   313  
   314  func (it *SimpleSlice) Count() int {
   315  	return it.Length()
   316  }
   317  
   318  func (it *SimpleSlice) CountFunc(
   319  	counterFunc func(index int, item string) (isCount bool),
   320  ) int {
   321  	if it.IsEmpty() {
   322  		return 0
   323  	}
   324  
   325  	counter := 0
   326  	for i, item := range it.Items {
   327  		isCount := counterFunc(i, item)
   328  
   329  		if isCount {
   330  			counter++
   331  		}
   332  	}
   333  
   334  	return counter
   335  }
   336  
   337  func (it *SimpleSlice) IsEmpty() bool {
   338  	return it == nil || it.Length() == 0
   339  }
   340  
   341  func (it *SimpleSlice) HasAnyItem() bool {
   342  	return !it.IsEmpty()
   343  }
   344  
   345  func (it *SimpleSlice) LastIndex() int {
   346  	return it.Length() - 1
   347  }
   348  
   349  func (it *SimpleSlice) HasIndex(index int) bool {
   350  	return it.LastIndex() >= index
   351  }
   352  
   353  func (it *SimpleSlice) Strings() []string {
   354  	return it.Items
   355  }
   356  
   357  func (it *SimpleSlice) Hashset() *Hashset {
   358  	return New.Hashset.StringsPtr(&it.Items)
   359  }
   360  
   361  func (it *SimpleSlice) Join(joiner string) string {
   362  	if it.IsEmpty() {
   363  		return ""
   364  	}
   365  
   366  	return strings.Join(it.Items, joiner)
   367  }
   368  
   369  func (it *SimpleSlice) JoinLine() string {
   370  	if it.IsEmpty() {
   371  		return ""
   372  	}
   373  
   374  	return strings.Join(
   375  		it.Items,
   376  		constants.DefaultLine)
   377  }
   378  
   379  // JoinLineEofLine
   380  //
   381  //  contains new line at the end of the file
   382  func (it *SimpleSlice) JoinLineEofLine() string {
   383  	if it.IsEmpty() {
   384  		return ""
   385  	}
   386  
   387  	joined := strings.Join(
   388  		it.Items,
   389  		constants.DefaultLine)
   390  
   391  	if strings.HasSuffix(joined, constants.DefaultLine) {
   392  		// already contains
   393  
   394  		return joined
   395  	}
   396  
   397  	return joined + constants.DefaultLine
   398  }
   399  
   400  func (it *SimpleSlice) JoinSpace() string {
   401  	return strings.Join(it.Items, constants.Space)
   402  }
   403  
   404  func (it *SimpleSlice) JoinComma() string {
   405  	return strings.Join(it.Items, constants.Comma)
   406  }
   407  
   408  func (it *SimpleSlice) JoinCsv() string {
   409  	return strings.Join(it.CsvStrings(), constants.Comma)
   410  }
   411  
   412  func (it *SimpleSlice) JoinCsvLine() string {
   413  	return strings.Join(it.CsvStrings(), constants.CommaUnixNewLine)
   414  }
   415  
   416  func (it *SimpleSlice) EachItemSplitBy(splitBy string) (splitItemsOnly []string) {
   417  	slice := make([]string, 0, it.Length()*constants.Capacity3)
   418  
   419  	for _, item := range it.Items {
   420  		splitItems := strings.Split(item, splitBy)
   421  
   422  		slice = append(slice, splitItems...)
   423  	}
   424  
   425  	return slice
   426  }
   427  
   428  func (it *SimpleSlice) PrependJoin(
   429  	joiner string,
   430  	prependItems ...string,
   431  ) string {
   432  	prependSlice := &SimpleSlice{
   433  		Items: prependItems,
   434  	}
   435  
   436  	return prependSlice.
   437  		ConcatNew(it.Items...).
   438  		Join(joiner)
   439  }
   440  
   441  func (it *SimpleSlice) AppendJoin(
   442  	joiner string,
   443  	appendItems ...string,
   444  ) string {
   445  	return it.
   446  		ConcatNew(appendItems...).
   447  		Join(joiner)
   448  }
   449  
   450  func (it *SimpleSlice) PrependAppend(
   451  	prependItems, appendItems []string,
   452  ) *SimpleSlice {
   453  	if len(prependItems) > 0 {
   454  		it.Items = append(prependItems, it.Items...)
   455  	}
   456  
   457  	if len(appendItems) > 0 {
   458  		it.Items = append(it.Items, appendItems...)
   459  	}
   460  
   461  	return it
   462  }
   463  
   464  func (it *SimpleSlice) IsEqual(another *SimpleSlice) bool {
   465  	if it == nil && another == nil {
   466  		return true
   467  	}
   468  
   469  	if it == nil || another == nil {
   470  		return false
   471  	}
   472  
   473  	if it.Length() != another.Length() {
   474  		return false
   475  	}
   476  
   477  	if it.Length() == 0 {
   478  		return true
   479  	}
   480  
   481  	return it.IsEqualLines(another.Items)
   482  }
   483  
   484  // IsEqualUnorderedLines
   485  //
   486  // Will sort both the list,
   487  // output will contain sorted lines
   488  func (it *SimpleSlice) IsEqualUnorderedLines(lines []string) bool {
   489  	if it == nil && lines == nil {
   490  		return true
   491  	}
   492  
   493  	if it == nil || lines == nil {
   494  		return false
   495  	}
   496  
   497  	if it.Length() != len(lines) {
   498  		return false
   499  	}
   500  
   501  	if it.Length() == 0 {
   502  		return true
   503  	}
   504  
   505  	sort.Strings(it.Items)
   506  	sort.Strings(lines)
   507  
   508  	for i, item := range it.Items {
   509  		anotherItem := lines[i]
   510  
   511  		if item != anotherItem {
   512  			return false
   513  		}
   514  	}
   515  
   516  	return true
   517  }
   518  
   519  // IsEqualUnorderedLinesClone
   520  //
   521  // Will sort both the list,
   522  // output will contain sorted lines
   523  func (it *SimpleSlice) IsEqualUnorderedLinesClone(lines []string) bool {
   524  	if it == nil && lines == nil {
   525  		return true
   526  	}
   527  
   528  	if it == nil || lines == nil {
   529  		return false
   530  	}
   531  
   532  	if it.Length() != len(lines) {
   533  		return false
   534  	}
   535  
   536  	if it.Length() == 0 {
   537  		return true
   538  	}
   539  
   540  	clonedLeftSlice := it.Clone(true)
   541  	clonedRightSlice := New.SimpleSlice.Direct(true, lines)
   542  
   543  	sort.Strings(clonedLeftSlice.Items)
   544  	sort.Strings(clonedRightSlice.Items)
   545  
   546  	for i, item := range clonedLeftSlice.Items {
   547  		anotherItem := clonedRightSlice.Items[i]
   548  
   549  		if item != anotherItem {
   550  			return false
   551  		}
   552  	}
   553  
   554  	go func() {
   555  		clonedLeftSlice.Dispose()
   556  		clonedRightSlice.Dispose()
   557  	}()
   558  
   559  	return true
   560  }
   561  
   562  func (it *SimpleSlice) IsEqualLines(lines []string) bool {
   563  	if it == nil && lines == nil {
   564  		return true
   565  	}
   566  
   567  	if it == nil || lines == nil {
   568  		return false
   569  	}
   570  
   571  	if it.Length() != len(lines) {
   572  		return false
   573  	}
   574  
   575  	for i, item := range it.Items {
   576  		anotherItem := lines[i]
   577  
   578  		if item != anotherItem {
   579  			return false
   580  		}
   581  	}
   582  
   583  	return true
   584  }
   585  
   586  func (it *SimpleSlice) Collection(isClone bool) *Collection {
   587  	return New.Collection.StringsOptions(
   588  		isClone,
   589  		it.Items,
   590  	)
   591  }
   592  
   593  func (it SimpleSlice) NonPtr() SimpleSlice {
   594  	return it
   595  }
   596  
   597  func (it *SimpleSlice) Ptr() *SimpleSlice {
   598  	return it
   599  }
   600  
   601  func (it *SimpleSlice) String() string {
   602  	if it.IsEmpty() {
   603  		return constants.EmptyString
   604  	}
   605  
   606  	return strings.Join(
   607  		it.Items,
   608  		constants.NewLineUnix)
   609  }
   610  
   611  func (it *SimpleSlice) ConcatNewSimpleSlices(items ...*SimpleSlice) *SimpleSlice {
   612  	items2 := append(
   613  		items,
   614  		it)
   615  	length := AllIndividualsLengthOfSimpleSlices(items2...)
   616  	slice := make(
   617  		[]string,
   618  		0,
   619  		length)
   620  
   621  	slice = append(slice, it.Items...)
   622  
   623  	for _, simpleSlice := range items {
   624  		slice = append(slice, simpleSlice.Items...)
   625  	}
   626  
   627  	return &SimpleSlice{Items: slice}
   628  }
   629  
   630  func (it *SimpleSlice) ConcatNewStrings(items ...string) []string {
   631  	if it == nil {
   632  		return CloneSlice(items)
   633  	}
   634  
   635  	slice := make(
   636  		[]string,
   637  		0,
   638  		it.Length()+len(items))
   639  
   640  	slice = append(slice, it.Items...)
   641  	slice = append(slice, items...)
   642  
   643  	return slice
   644  }
   645  
   646  func (it *SimpleSlice) ConcatNew(items ...string) *SimpleSlice {
   647  	concatNew := it.ConcatNewStrings(items...)
   648  
   649  	return &SimpleSlice{
   650  		concatNew,
   651  	}
   652  }
   653  
   654  func (it *SimpleSlice) ToCollection(isClone bool) *Collection {
   655  	return New.Collection.StringsOptions(isClone, it.Items)
   656  }
   657  
   658  func (it *SimpleSlice) CsvStrings() []string {
   659  	if it.IsEmpty() {
   660  		return []string{}
   661  	}
   662  
   663  	newSlice := make([]string, it.Length())
   664  
   665  	for i, item := range it.Items {
   666  		newSlice[i] = fmt.Sprintf(
   667  			constants.SprintDoubleQuoteFormat,
   668  			item)
   669  	}
   670  
   671  	return newSlice
   672  }
   673  
   674  func (it *SimpleSlice) JoinCsvString(joiner string) string {
   675  	if it.IsEmpty() {
   676  		return ""
   677  	}
   678  
   679  	newSlice := it.CsvStrings()
   680  
   681  	return strings.Join(newSlice, joiner)
   682  }
   683  
   684  func (it *SimpleSlice) JoinWith(
   685  	joiner string,
   686  ) string {
   687  	if it.IsEmpty() {
   688  		return ""
   689  	}
   690  
   691  	return joiner + strings.Join(it.Items, joiner)
   692  }
   693  
   694  func (it SimpleSlice) JsonModel() []string {
   695  	return it.Items
   696  }
   697  
   698  func (it *SimpleSlice) Sort() *SimpleSlice {
   699  	sort.Strings(it.Items)
   700  
   701  	return it
   702  }
   703  
   704  func (it *SimpleSlice) Reverse() *SimpleSlice {
   705  	length := it.Length()
   706  
   707  	if length <= 1 {
   708  		return it
   709  	}
   710  
   711  	if length == 2 {
   712  		it.Items[0], it.Items[1] = it.Items[1], it.Items[0]
   713  
   714  		return it
   715  	}
   716  
   717  	mid := length / 2
   718  	lastIndex := length - 1
   719  
   720  	for i := 0; i < mid; i++ {
   721  		first := it.Items[i]
   722  		it.Items[i] = it.Items[lastIndex-i]
   723  		it.Items[lastIndex-i] = first
   724  	}
   725  
   726  	return it
   727  }
   728  
   729  func (it SimpleSlice) JsonModelAny() interface{} {
   730  	return it.JsonModel()
   731  }
   732  
   733  func (it SimpleSlice) MarshalJSON() ([]byte, error) {
   734  	return json.Marshal(it.JsonModel())
   735  }
   736  
   737  func (it *SimpleSlice) UnmarshalJSON(
   738  	rawBytes []byte,
   739  ) error {
   740  	var dataModel []string
   741  	err := json.Unmarshal(rawBytes, &dataModel)
   742  
   743  	if err == nil {
   744  		it.Items = dataModel
   745  	}
   746  
   747  	return err
   748  }
   749  
   750  func (it SimpleSlice) Json() corejson.Result {
   751  	return corejson.New(it)
   752  }
   753  
   754  func (it SimpleSlice) JsonPtr() *corejson.Result {
   755  	return corejson.NewPtr(it)
   756  }
   757  
   758  func (it *SimpleSlice) ParseInjectUsingJson(
   759  	jsonResult *corejson.Result,
   760  ) (*SimpleSlice, error) {
   761  	err := jsonResult.Unmarshal(it)
   762  
   763  	if err != nil {
   764  		return Empty.SimpleSlice(), err
   765  	}
   766  
   767  	return it, nil
   768  }
   769  
   770  // ParseInjectUsingJsonMust Panic if error
   771  func (it *SimpleSlice) ParseInjectUsingJsonMust(
   772  	jsonResult *corejson.Result,
   773  ) *SimpleSlice {
   774  	parsedResult, err := it.
   775  		ParseInjectUsingJson(jsonResult)
   776  
   777  	if err != nil {
   778  		panic(err)
   779  	}
   780  
   781  	return parsedResult
   782  }
   783  
   784  func (it SimpleSlice) AsJsonContractsBinder() corejson.JsonContractsBinder {
   785  	return &it
   786  }
   787  
   788  func (it SimpleSlice) AsJsoner() corejson.Jsoner {
   789  	return &it
   790  }
   791  
   792  func (it SimpleSlice) ToPtr() *SimpleSlice {
   793  	return &it
   794  }
   795  
   796  func (it SimpleSlice) ToNonPtr() SimpleSlice {
   797  	return it
   798  }
   799  
   800  func (it *SimpleSlice) JsonParseSelfInject(
   801  	jsonResult *corejson.Result,
   802  ) error {
   803  	_, err := it.ParseInjectUsingJson(
   804  		jsonResult,
   805  	)
   806  
   807  	return err
   808  }
   809  
   810  func (it SimpleSlice) AsJsonParseSelfInjector() corejson.JsonParseSelfInjector {
   811  	return &it
   812  }
   813  
   814  func (it SimpleSlice) AsJsonMarshaller() corejson.JsonMarshaller {
   815  	return &it
   816  }
   817  
   818  func (it *SimpleSlice) Clear() *SimpleSlice {
   819  	if it == nil {
   820  		return nil
   821  	}
   822  
   823  	it.Items = it.Items[:0]
   824  
   825  	return it
   826  }
   827  
   828  func (it *SimpleSlice) Dispose() {
   829  	if it == nil {
   830  		return
   831  	}
   832  
   833  	it.Clear()
   834  	it.Items = nil
   835  }
   836  
   837  func (it SimpleSlice) Clone(isDeepClone bool) SimpleSlice {
   838  	return SimpleSlice{
   839  		Items: CloneSliceIf(
   840  			isDeepClone,
   841  			it.Items...),
   842  	}
   843  }
   844  
   845  func (it *SimpleSlice) ClonePtr(isDeepClone bool) *SimpleSlice {
   846  	if it == nil {
   847  		return nil
   848  	}
   849  
   850  	cloned := it.Clone(
   851  		isDeepClone)
   852  
   853  	return &cloned
   854  }
   855  
   856  func (it SimpleSlice) DeepClone() *SimpleSlice {
   857  	return it.ClonePtr(true)
   858  }
   859  
   860  func (it SimpleSlice) ShadowClone() *SimpleSlice {
   861  	return it.ClonePtr(false)
   862  }
   863  
   864  func (it SimpleSlice) IsDistinctEqualRaw(rightLines ...string) bool {
   865  	return it.Hashset().IsEqualsPtr(New.Hashset.Strings(rightLines))
   866  }
   867  
   868  func (it SimpleSlice) IsDistinctEqual(rightSlice *SimpleSlice) bool {
   869  	return it.Hashset().IsEqualsPtr(rightSlice.Hashset())
   870  }
   871  
   872  // IsUnorderedEqualRaw
   873  //
   874  //  sort both and then compare, if length are not equal then mismatch
   875  //
   876  //  isClone:
   877  //      Don't mutate, create copy and then sort and then returns the final result.
   878  func (it SimpleSlice) IsUnorderedEqualRaw(
   879  	isClone bool,
   880  	rightLines ...string,
   881  ) bool {
   882  	if it.Length() != len(rightLines) {
   883  		return false
   884  	}
   885  
   886  	if it.Length() == 0 {
   887  		// no checking required
   888  		return true
   889  	}
   890  
   891  	if isClone {
   892  		leftSored := it.DeepClone()
   893  		leftSored.Sort()
   894  
   895  		rightSort := New.SimpleSlice.Direct(true, rightLines)
   896  		rightSort.Sort()
   897  
   898  		return leftSored.IsEqual(rightSort)
   899  	}
   900  
   901  	it.Sort()
   902  	rightSort := New.SimpleSlice.Direct(
   903  		false,
   904  		rightLines)
   905  	rightSort.Sort()
   906  
   907  	return it.IsEqual(rightSort)
   908  }
   909  
   910  // IsUnorderedEqual
   911  //
   912  //  sort both and then compare, if length are not equal then mismatch
   913  //
   914  //  isClone:
   915  //      Don't mutate, create copy and then sort and then returns the final result.
   916  func (it SimpleSlice) IsUnorderedEqual(
   917  	isClone bool,
   918  	rightSlice *SimpleSlice,
   919  ) bool {
   920  	if it.IsEmpty() && rightSlice.IsEmpty() {
   921  		return true
   922  	}
   923  
   924  	if rightSlice == nil {
   925  		// is nil left is not empty
   926  		return false
   927  	}
   928  
   929  	return it.IsUnorderedEqualRaw(
   930  		isClone,
   931  		rightSlice.Items...)
   932  }
   933  
   934  // IsEqualByFunc
   935  //
   936  //  checks comparison by the given function.
   937  func (it SimpleSlice) IsEqualByFunc(
   938  	isMatchCheckerFunc func(index int, left, right string) (isMatch bool),
   939  	rightLines ...string,
   940  ) bool {
   941  	if it.Length() != len(rightLines) {
   942  		return false
   943  	}
   944  
   945  	if it.Length() == 0 {
   946  		return true
   947  	}
   948  
   949  	for i, rightLine := range rightLines {
   950  		leftLine := it.Items[i]
   951  
   952  		if !isMatchCheckerFunc(i, leftLine, rightLine) {
   953  			return false
   954  		}
   955  	}
   956  
   957  	return true
   958  }
   959  
   960  // IsEqualByFuncLinesSplit
   961  //
   962  //  first splits the line and then takes
   963  //  lines and process same as EqualByFunc
   964  func (it SimpleSlice) IsEqualByFuncLinesSplit(
   965  	isTrim bool,
   966  	splitter string,
   967  	rightLine string,
   968  	isMatchCheckerFunc func(index int, left, right string) (isMatch bool),
   969  ) bool {
   970  	rightLines := strings.Split(rightLine, splitter)
   971  
   972  	if len(rightLines) != it.Length() {
   973  		return false
   974  	}
   975  
   976  	if it.IsEmpty() {
   977  		return true
   978  	}
   979  
   980  	for i, curLeftLine := range it.Items {
   981  		curRightLine := rightLines[i]
   982  
   983  		if isTrim {
   984  			curLeftLine = strings.TrimSpace(curLeftLine)
   985  			curRightLine = strings.TrimSpace(curRightLine)
   986  		}
   987  
   988  		if !isMatchCheckerFunc(i, curLeftLine, curRightLine) {
   989  			return false
   990  		}
   991  	}
   992  
   993  	return true
   994  }
   995  
   996  func (it *SimpleSlice) DistinctDiffRaw(
   997  	rightLines ...string,
   998  ) []string {
   999  	if it == nil && rightLines == nil {
  1000  		return []string{}
  1001  	}
  1002  
  1003  	if it == nil && rightLines != nil {
  1004  		return rightLines
  1005  	}
  1006  
  1007  	if it != nil && rightLines == nil {
  1008  		return it.Items
  1009  	}
  1010  
  1011  	return New.
  1012  		Hashset.
  1013  		Strings(it.Items).
  1014  		DistinctDiffLinesRaw(rightLines...)
  1015  }
  1016  
  1017  func (it *SimpleSlice) AddedRemovedLinesDiff(
  1018  	rightLines ...string,
  1019  ) (addedLines, removedLines []string) {
  1020  	if it == nil && rightLines == nil {
  1021  		return addedLines, removedLines
  1022  	}
  1023  
  1024  	oldDomainsHashSet := New.Hashset.Strings(it.SafeStrings())
  1025  	newDomainsHashSet := New.Hashset.Strings(rightLines)
  1026  
  1027  	addedDomainsPtr := newDomainsHashSet.
  1028  		GetAllExceptHashset(oldDomainsHashSet)
  1029  	deletedDomainsPtr := oldDomainsHashSet.
  1030  		GetAllExceptHashset(newDomainsHashSet)
  1031  
  1032  	if addedDomainsPtr != nil {
  1033  		addedLines = addedDomainsPtr
  1034  	}
  1035  
  1036  	if deletedDomainsPtr != nil {
  1037  		removedLines = deletedDomainsPtr
  1038  	}
  1039  
  1040  	return addedLines, removedLines
  1041  
  1042  }
  1043  
  1044  func (it *SimpleSlice) DistinctDiff(
  1045  	rightSlice *SimpleSlice,
  1046  ) []string {
  1047  	if it == nil && rightSlice == nil {
  1048  		return []string{}
  1049  	}
  1050  
  1051  	if it == nil && rightSlice != nil {
  1052  		return rightSlice.Items
  1053  	}
  1054  
  1055  	if it != nil && rightSlice == nil {
  1056  		return it.Items
  1057  	}
  1058  
  1059  	return New.
  1060  		Hashset.
  1061  		Strings(it.Items).
  1062  		DistinctDiffLinesRaw(rightSlice.Items...)
  1063  }
  1064  
  1065  func (it *SimpleSlice) Serialize() ([]byte, error) {
  1066  	return corejson.Serialize.Raw(it)
  1067  }
  1068  
  1069  func (it *SimpleSlice) Deserialize(toPtr interface{}) (parsingErr error) {
  1070  	return it.JsonPtr().Deserialize(toPtr)
  1071  }
  1072  
  1073  func (it *SimpleSlice) SafeStrings() []string {
  1074  	if it == nil || it.Items == nil {
  1075  		return []string{}
  1076  	}
  1077  
  1078  	return it.Items
  1079  }