csvcheck

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2024 License: MIT Imports: 4 Imported by: 0

README

csvcheck

A Go module for comparing the rows of different csv arrays with a bundle of convenient functions to help.

Installation

Install the latest version of csvcheck using

go get -u github.com/BrianWeiHaoMa/csvcheck@latest

To include csvcheck in your program

import "github.com/BrianWeiHaoMa/csvcheck"

Example 1:

// First csv array.
arr1 := csvcheck.Get2DArrayFrom2DArray([][]string{
    {"a", "b", "c"},
    {"1", "2", "3"},
    {"4", "5", "6"},
    {"7", "8", "9"},
    {"1", "2", "3"},
    {"7", "8", "9"},
})

// Second csv array.
arr2 := csvcheck.Get2DArrayFrom2DArray([][]string{
    {"a", "b", "c"},
    {"x", "y", "z"},
    {"1", "2", "3"},
    {"4", "5", "6"},
    {"7", "8", "9"},
    {"10", "11", "12"},
})

// Returns the different rows and their indices in their
// original arrays, respectively.
res1, res2, indices1, indices2, _ := csvcheck.GetCommonRows(
    arr1,
    arr2,
    csvcheck.Options{
        SortIndices: true,
        Method:      csvcheck.MethodSet,
        // Can choose one of MethodDirect, MethodSet, and MethodMatch.
    },
)

res1String, _ := csvcheck.StringFormatCsvArray(res1)
res2String, _ := csvcheck.PrettyFormatCsvArray(res2, 3, -1)

fmt.Println(indices1)
fmt.Println(res1String)

fmt.Println(indices2)
fmt.Println(res2String)
Output 1:
[0 1 2 3]
a,b,c
1,2,3
4,5,6
7,8,9

[0 2 3 4]
a   b   c
1   2   3
4   5   6
7   8   9

Example 2:

arr1 := csvcheck.Get2DArrayFrom2DArray([][]string{
    {"a", "b", "c"},
    {"1", "2", "3"},
    {"4", "5", "6"},
    {"7", "8", "9"},
    {"1", "2", "3"},
    {"7", "8", "9"},
})
arr2 := csvcheck.Get2DArrayFrom2DArray([][]string{
    {"a", "x", "y"},
    {"x", "y", "z"},
    {"1", "2", "3"},
    {"4", "5", "6"},
    {"7", "8", "9"},
    {"10", "11", "12"},
})

res1, res2, indices1, indices2, _ := csvcheck.GetDifferentRows(
    arr1,
    arr2,
    csvcheck.Options{
        SortIndices: true,
        Method:      csvcheck.MethodMatch,
        UseColumns: csvcheck.GetRowFromRow([]string{"a"}),
    },
)

res1String, _ := csvcheck.StringFormatCsvArray(res1)
res2String, _ := csvcheck.PrettyFormatCsvArray(res2, 3, -1)

fmt.Println(indices1)
fmt.Println(res1String)

fmt.Println(indices2)
fmt.Println(res2String)
Output 2:
[0 4 5]
a,b,c
1,2,3
7,8,9

[0 1 5]
a    x    y
x    y    z
10   11   12

Notes

GetCommonRows
  • MethodDirect: Compares each row of arr1 with the row in arr2 at the same index.
  • MethodSet: Iff the row exists in arr1 and arr2 (ignoring indices), the row is kept.
  • MethodMatch: Matches rows from arr1 to arr2 from the top down. Only rows that can be matched are kept.
GetDifferentRows
  • MethodDirect: Compares each row of arr1 with the row in arr2 at the same index.
  • MethodSet: Iff the row doesn't exist in the other array keep it in the result for the current array.
  • MethodMatch: All the rows not returned by GetCommonRows using MethodMatch, respectively.

Documentation

Index

Constants

View Source
const (
	MethodMatch = iota
	MethodDirect
	MethodSet
)

Supported comparison methods.

View Source
const TruncatedMark = ".."

For marking truncated pretty formatted strings.

Variables

This section is empty.

Functions

func AutoAlignCsvArrays

func AutoAlignCsvArrays(csvArray1, csvArray2 [][]StringHashable) ([][]StringHashable, [][]StringHashable, error)

Automatically aligns the common columns on the left side. The relative positions of the common columns are the same as that of csvArray1 and uncommon columns are placed at the end with the same relative positions as that of the original arrays, respectively.

func CheckForProperCsvArray

func CheckForProperCsvArray(arr [][]StringHashable) error

Returns nil iff the array is not empty, has no duplicate columns, and all rows have the same number elements.

func Get2DArrayFrom2DArray

func Get2DArrayFrom2DArray(arr [][]string) [][]StringHashable

Returns a 2D array of StringHashable from a 2D array of strings.

func GetCommonIndices

func GetCommonIndices(arr1, arr2 [][]StringHashable, method int, sortIndices bool) ([]int, []int, error)

Returns the indices of rows common to both arrays based on the method given.

func GetCommonRows

func GetCommonRows(csvArray1, csvArray2 [][]StringHashable, options Options) ([][]StringHashable, [][]StringHashable, []int, []int, error)

Returns the common rows between the two arrays based on the given options and the indices of the rows in the results from the original arrays.

func GetDifferentIndices

func GetDifferentIndices(arr1, arr2 [][]StringHashable, method int, sortIndices bool) ([]int, []int, error)

Returns the indices of rows that are different between the two arrays based on the method given.

func GetDifferentRows

func GetDifferentRows(csvArray1, csvArray2 [][]StringHashable, options Options) ([][]StringHashable, [][]StringHashable, []int, []int, error)

Returns the different rows between the two arrays based on the given options and the indices of the rows in the results from the original arrays.

func IgnoreColumns

func IgnoreColumns(arr [][]StringHashable, columns []StringHashable) ([][]StringHashable, error)

Returns a new csv array with the columns specified removed.

func IgnoreRows

func IgnoreRows(arr [][]StringHashable, rows []int) ([][]StringHashable, error)

Returns a new 2D array with the rows specified removed.

func KeepColumns

func KeepColumns(arr [][]StringHashable, columns []StringHashable) ([][]StringHashable, error)

Returns a new csv array with only the columns specified.

func KeepRows

func KeepRows(arr [][]StringHashable, rows []int) ([][]StringHashable, error)

Returns a new 2D array with only the rows specified.

func PrettyFormatCsvArray

func PrettyFormatCsvArray(csvArray [][]StringHashable, spaces int, maxColLength int) (string, error)

Takes a csv array and returns a columns-aligned formatted string according to spaces. Strings with lengths exceeding maxColLength are truncated. Use a negative value for maxColLength to keep strings of all lengths.

func RearrangeColumns

func RearrangeColumns(arr [][]StringHashable, columns []StringHashable) ([][]StringHashable, error)

Returns a csv array with the columns rearranged accordingly.

func StringFormatCsvArray

func StringFormatCsvArray(csvArray [][]StringHashable) (string, error)

Takes a csv array and returns a csv formatted string.

Types

type BasicStringHashable

type BasicStringHashable string

A basic implementation of the StringHashable interface for convenience.

func (BasicStringHashable) StringHash

func (s BasicStringHashable) StringHash() string

type Options

type Options struct {
	Method        int
	UseColumns    []StringHashable
	IgnoreColumns []StringHashable
	SortIndices   bool
}

For holding supported options.

func (*Options) CheckAttributes

func (o *Options) CheckAttributes() error

Checks if the options are valid.

type StringHashable

type StringHashable interface {
	StringHash() string // This is used for equality comparison purposes.
}

For allowing different types to be used in the csv arrays.

func GetCommonColumns

func GetCommonColumns(csvArray1, csvArray2 [][]StringHashable) ([]StringHashable, error)

Returns the common columns between the two csv arrays keeping the order of the relative positions of the columns in csvArray1.

func GetRowFromRow

func GetRowFromRow(arr []string) []StringHashable

Returns a StringHashable row from a string row.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL