utils

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package utils provides utility functions for commonmeta.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelCaseString added in v0.3.1

func CamelCaseString(str string) string

CamelCaseString converts a pascal case string to camel case

func CamelCaseToWords added in v0.2.14

func CamelCaseToWords(str string) string
Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.CamelCaseToWords("earthAndRelatedEnvironmentalSciences")
	fmt.Println(s)
}
Output:

Earth and related environmental sciences

func CheckORCIDNumberRange added in v0.19.0

func CheckORCIDNumberRange(orcid string) bool

check if ORCID is in the range 0000-0001-5000-0007 and 0000-0003-5000-0001 or between 0009-0000-0000-0000 and 0009-0010-0000-0000

func CommunitySlugAsURL added in v0.5.16

func CommunitySlugAsURL(slug string, host string) string

CommunitySlugAsURL returns the InvenioRDM community slug expressed as globally unique URL

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.CommunitySlugAsURL("irights", "rogue-scholar.org")
	fmt.Println(s)
}
Output:

https://rogue-scholar.org/api/communities/irights

func DecodeID added in v0.6.30

func DecodeID(id string) (int64, error)
Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.DecodeID("https://doi.org/10.59350/b8pcg-q9k70")
	fmt.Println(s)
}
Output:

387298385203

func FindFromFormat

func FindFromFormat(p Params) string

FindFromFormat finds the commonmeta read format

func FindFromFormatByExt

func FindFromFormatByExt(ext string) string

FindFromFormatByExt finds the commonmeta reader from format by file extension

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.FindFromFormatByExt(".bib")
	fmt.Println(s)
}
Output:

bibtex

func FindFromFormatByFilename

func FindFromFormatByFilename(filename string) string

FindFromFormatByFilename finds the commonmeta reader from format by filename

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.FindFromFormatByFilename("CITATION.cff")
	fmt.Println(s)
}
Output:

cff

func FindFromFormatByID

func FindFromFormatByID(id string) string

FindFromFormatByID finds the commonmeta reader from format by id

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.FindFromFormatByID("https://api.rogue-scholar.org/posts/4e4bf150-751f-4245-b4ca-fe69e3c3bb24")
	fmt.Println(s)
}
Output:

jsonfeed

func FindFromFormatByMap added in v0.6.48

func FindFromFormatByMap(m map[string]interface{}) string

FindFromFormatByMap finds the commonmeta reader from format by map

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	f := map[string]any{
		"schemaVersion": "http://datacite.org/schema/kernel-4",
	}
	s := utils.FindFromFormatByMap(f)
	fmt.Println(s)
}
Output:

datacite

func FindFromFormatByString

func FindFromFormatByString(str string) string

FindFromFormatByString finds the commonmeta reader from format by string

func GetLanguage added in v0.5.1

func GetLanguage(lang string, format string) string
Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.GetLanguage("de", "iso639-3")
	fmt.Println(s)
}
Output:

deu

func ISSNAsURL added in v0.2.1

func ISSNAsURL(issn string) string

ISSNAsURL returns the ISSN expressed as URL

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.ISSNAsURL("2146-8427")
	fmt.Println(s)
}
Output:

https://portal.issn.org/resource/ISSN/2146-8427

func KebabCaseToCamelCase added in v0.6.10

func KebabCaseToCamelCase(str string) string

KebabCaseToCamelCase converts a kebab case string to camel case

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.KebabCaseToCamelCase("earth-and-related-environmental-sciences")
	fmt.Println(s)
}
Output:

earthAndRelatedEnvironmentalSciences

func KebabCaseToPascalCase added in v0.6.10

func KebabCaseToPascalCase(str string) string

KebabCaseToPascalCase converts a kebab case string to pascal case

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.KebabCaseToPascalCase("earth-and-related-environmental-sciences")
	fmt.Println(s)
}
Output:

EarthAndRelatedEnvironmentalSciences

func NormalizeCCUrl

func NormalizeCCUrl(url_ string) (string, bool)

NormalizeCCUrl returns the normalized Creative Commons License URL

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, ok := utils.NormalizeCCUrl("https://creativecommons.org/licenses/by/4.0")
	fmt.Println(s, ok)
}
Output:

https://creativecommons.org/licenses/by/4.0/legalcode true

func NormalizeID

func NormalizeID(pid string) string

NormalizeID checks for valid DOI or HTTP(S) URL and normalizes them

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.NormalizeID("10.1101/097196")
	fmt.Println(s)
}
Output:

https://doi.org/10.1101/097196

func NormalizeORCID

func NormalizeORCID(orcid string) string

NormalizeORCID returns a normalized ORCID URL

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.NormalizeORCID("0000-0002-1825-0097")
	fmt.Println(s)
}
Output:

https://orcid.org/0000-0002-1825-0097

func NormalizeOrganizationID added in v0.19.0

func NormalizeOrganizationID(id string) string

NormalizeOrganizationID normalizes organization ID

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.NormalizeOrganizationID("https://ror.org/0342dzm54")
	fmt.Println(s)
}
Output:

https://ror.org/0342dzm54

func NormalizePersonID added in v0.19.0

func NormalizePersonID(id string) string

NormalizePersonID normalizes person ID

func NormalizeROR

func NormalizeROR(ror string) string

NormalizeROR returns a normalized ROR URL

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.NormalizeROR("http://ror.org/0342dzm54")
	fmt.Println(s)
}
Output:

https://ror.org/0342dzm54

func NormalizeString added in v0.10.3

func NormalizeString(s string) (string, error)
Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.NormalizeString("Investigación-Digital💿")
	fmt.Println(s)
}
Output:

Investigacion-Digital💿

func NormalizeURL added in v0.2.1

func NormalizeURL(str string, secure bool, lower bool) (string, error)

NormalizeURL normalizes URL

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.NormalizeURL("http://elifesciences.org/Articles/91729", true, true)
	fmt.Println(s)
}
Output:

https://elifesciences.org/articles/91729

func NormalizeWorkID added in v0.19.0

func NormalizeWorkID(id string) string

NormalizeWorkID normalizes work ID

func ParseString added in v0.6.10

func ParseString(s interface{}) string

ParseString parses an interface into a string

func SPDXToURL added in v0.6.49

func SPDXToURL(id string) string

SPDXToURL provides the SPDX license URL given a Creative Commons SPDX ID

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.SPDXToURL("CC-BY-4.0")
	fmt.Println(s)
}
Output:

https://creativecommons.org/licenses/by/4.0/legalcode

func Sanitize

func Sanitize(html string) string

Sanitize removes all HTML tags except for a whitelist of allowed tags. Used for title and description fields.

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.Sanitize("<p>The Origins of SARS-CoV-2: A <i>Critical</i> <a href=\"index.html\">Review</a></p>")
	fmt.Println(s)
}
Output:

The Origins of SARS-CoV-2: A <i>Critical</i> Review

func SplitString added in v0.19.0

func SplitString(str string, n int, s string) string

func SplitString adds a hyphen every n characters

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.SplitString("0000000121122291", 4, " ")
	fmt.Println(s)
}
Output:

0000 0001 2112 2291

func StringToSlug added in v0.10.3

func StringToSlug(str string) string

StringToSlug makes a string lowercase and removes non-alphanumeric characters

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.StringToSlug("Investigación-Digital💿")
	fmt.Println(s)
}
Output:

investigaciondigital

func TitleCase added in v0.2.8

func TitleCase(str string) string

TitleCase capitalizes the first letter of a string without changing the rest

func URLToSPDX added in v0.2.1

func URLToSPDX(url string) string

URLToSPDX provides the SPDX license ID given a Creative Commons URL

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.URLToSPDX("https://creativecommons.org/licenses/by/4.0/legalcode")
	fmt.Println(s)
}
Output:

CC-BY-4.0

func UnescapeUTF8 added in v0.2.1

func UnescapeUTF8(inStr string) (outStr string, err error)

UnescapeUTF8 unescapes UTF-8 characters

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.UnescapeUTF8("capable of signi\"cance.")
	fmt.Println(s)
}
Output:

capable of signi"cance.

func ValidateCrossrefFunderID added in v0.19.0

func ValidateCrossrefFunderID(fundref string) (string, bool)
Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.ValidateCrossrefFunderID("https://doi.org/10.13039/501100001659")
	fmt.Println(s)
}
Output:

501100001659

func ValidateGRID added in v0.19.0

func ValidateGRID(grid string) (string, bool)

ValidateGRID validates a GRID ID GRID ID is a string prefixed with grid followed by dot number dot string

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.ValidateGRID("https://www.grid.ac/institutes/grid.1017.7")
	fmt.Println(s)
}
Output:

grid.1017.7

func ValidateID added in v0.2.14

func ValidateID(id string) (string, string)

ValidateID validates an identifier and returns the type Can be DOI, UUID, ISSN, ORCID, ROR, URL, RID, Wikidata, ISNI or GRID

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, t := utils.ValidateID("https://ror.org/0342dzm54")
	fmt.Println(s, t)
}
Output:

0342dzm54 ROR

func ValidateIDCategory added in v0.19.0

func ValidateIDCategory(id string) (string, string, string)

ValidateIDCategory validates an identifier and returns the identifier, type, and category Category can be work, person, organization, contributor (person or organization), or all

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	_, _, s := utils.ValidateIDCategory("https://ror.org/0342dzm54")
	fmt.Println(s)
}
Output:

Organization

func ValidateISNI added in v0.19.0

func ValidateISNI(isni string) (string, bool)

ValidateISNI validates an ISNI ISNI is a 16-character string in blocks of four optionally separated by hyphens or spaces and NOT between 0000-0001-5000-0007 and 0000-0003-5000-0001, or between 0009-0000-0000-0000 and 0009-0010-0000-0000 (the ranged reserved for ORCID).

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.ValidateISNI("https://isni.org/isni/000000012146438X")
	fmt.Println(s)
}
Output:

000000012146438X

func ValidateISSN added in v0.2.14

func ValidateISSN(issn string) (string, bool)

ValidateISSN validates an ISSN

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.ValidateISSN("https://portal.issn.org/resource/ISSN/2146-8427")
	fmt.Println(s)
}
Output:

2146-8427

func ValidateORCID

func ValidateORCID(orcid string) (string, bool)

ValidateORCID validates an ORCID ORCID is a 16-character string in blocks of four separated by hyphens between 0000-0001-5000-0007 and 0000-0003-5000-0001, or between 0009-0000-0000-0000 and 0009-0010-0000-0000.

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.ValidateORCID("https://orcid.org/0000-0002-1825-0097")
	fmt.Println(s)
}
Output:

0000-0002-1825-0097

func ValidateRID added in v0.6.65

func ValidateRID(rid string) (string, bool)

ValidateRID validates a RID RID is the unique identifier used by the InvenioRDM platform

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.ValidateRID("nryd8-14284")
	fmt.Println(s)
}
Output:

nryd8-14284

func ValidateROR

func ValidateROR(ror string) (string, bool)

ValidateROR validates a ROR ID. The ROR ID starts with 0 followed by a 6-character alphanumeric string which is base32-encoded and a 2-digit checksum.

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.ValidateROR("https://ror.org/0342dzm54")
	fmt.Println(s)
}
Output:

0342dzm54

func ValidateURL added in v0.2.1

func ValidateURL(str string) string

ValidateURL validates a URL and checks if it is a DOI

func ValidateUUID added in v0.2.14

func ValidateUUID(uuid string) (string, bool)

ValidateUUID validates a UUID

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.ValidateUUID("2491b2d5-7daf-486b-b78b-e5aab48064c1")
	fmt.Println(s)
}
Output:

2491b2d5-7daf-486b-b78b-e5aab48064c1

func ValidateWikidata added in v0.19.0

func ValidateWikidata(wikidata string) (string, bool)

ValidateWikidata validates a Wikidata item ID Wikidata item ID is a string prefixed with Q followed by a number

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.ValidateWikidata("https://www.wikidata.org/wiki/Q7186")
	fmt.Println(s)
}
Output:

Q7186

func WordsToCamelCase added in v0.6.49

func WordsToCamelCase(str string) string

CamelCaseString converts a pascal case string to camel case

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s := utils.WordsToCamelCase("Earth and related environmental sciences")
	fmt.Println(s)
}
Output:

earthAndRelatedEnvironmentalSciences

Types

type Params

type Params struct {
	Pid, Str, Ext, Filename string
	Map                     map[string]interface{}
}

type ROR added in v0.2.14

type ROR struct {
	ID          string   `json:"id"`
	Name        string   `json:"name"`
	Types       []string `json:"types"`
	ExternalIds struct {
		FundRef struct {
			Preferred string   `json:"preferred"`
			All       []string `json:"all"`
		} `json:"FundRef"`
	} `json:"external_ids"`
}

ROR represents a Research Organization Registry (ROR) record

func GetROR added in v0.2.14

func GetROR(ror string) (ROR, error)

GetROR

Example
package main

import (
	"fmt"

	"github.com/front-matter/commonmeta/utils"
)

func main() {
	s, _ := utils.GetROR("https://ror.org/0342dzm54")
	fmt.Println(s.Name)
}
Output:

Liberate Science

Jump to

Keyboard shortcuts

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