Skip to content

Commit

Permalink
Add WriteFile and WriteFileStr functions.
Browse files Browse the repository at this point in the history
Signed-off-by: Aliwoto <[email protected]>
  • Loading branch information
ALiwoto committed Nov 4, 2024
1 parent 3dd805e commit 552068a
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions ssg/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package ssg

import (
"os"
"path/filepath"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -470,14 +472,17 @@ func NumGeneratorFrom[T rangeValues.Integer](from T) *NumIdGenerator[T] {
}
}

// IsAllLower returns true if the given string is all lower case.
func IsAllLower(value string) bool {
return strings.ToLower(value) == value
}

// IsAllUpper returns true if the given string is all upper case.
func IsAllUpper(value string) bool {
return strings.ToUpper(value) == value
}

// IsAllNumber returns true if the given string is all number.
func IsAllNumber(str string) bool {
for _, s := range str {
if !IsRuneNumber(s) {
Expand All @@ -488,6 +493,7 @@ func IsAllNumber(str string) bool {
return true
}

// IsAllNumbers returns true if all the given strings are all number.
func IsAllNumbers(str ...string) bool {
for _, ss := range str {
if !IsAllNumber(ss) {
Expand Down Expand Up @@ -530,6 +536,60 @@ func IsRuneNumber(r rune) bool {
return false
}

// WriteFile will write the given content to the specified file.
// If the file does not exist, it will create it.
// Please do note that this function will discard the error returned
// by file.Close, so if you need that, you should write the
// function yourself in your own code.
func WriteFileStr(path string, content string) error {
// Create the directory if it doesn't exist
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
return err
}

// Write the content to the file
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()

_, err = file.WriteString(content)
if err != nil {
return err
}

return nil
}

// WriteFile will write the given content to the specified file.
// If the file does not exist, it will create it.
// Please do note that this function will discard the error returned
// by file.Close, so if you need that, you should write the
// function yourself in your own code.
func WriteFile(path string, content []byte) error {
// Create the directory if it doesn't exist
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
return err
}

// Write the content to the file
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()

_, err = file.Write(content)
if err != nil {
return err
}

return nil
}

func repairString(value string) string {
entered := false
ignoreNext := false
Expand Down

0 comments on commit 552068a

Please sign in to comment.