Skip to content

Commit

Permalink
v2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
thinknathan committed Dec 15, 2023
1 parent f9693fa commit 9c021d2
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 59 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

# Defold Project Settings
*.project linguist-language=INI
*.script_api linguist-language=YAML

# Exclude files from archive
.gitattributes export-ignore
.prettierrc.json export-ignore
.vscode export-ignore
2 changes: 1 addition & 1 deletion @types/estring-1.0.0.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/** @noSelfInFile */

/**
* eString native extension for concatenation, trimming, splitting, padding, formatting time, and formatting numbers
* Defold native extension with functions for string manipulation in Lua scripts.
* @noResolution
*/
declare namespace estring {
Expand Down
27 changes: 15 additions & 12 deletions @types/estring-2.0.0.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
/** @noSelfInFile */

/**
* eString native extension for concatenation, trimming, splitting, padding, formatting time, and formatting numbers
* Defold native extension with functions for string manipulation in Lua scripts.
* @noResolution
*/
declare namespace estring {
/**
* Joins strings or numbers into a single string
* Joins strings or numbers and returns the result.
* @param args
*/
export function concat(...args: Array<string | number>): string;

/**
* Prettily formats a number, such as `1000` becomes `1,000`
* Formats a number as a string with options for precision, thousands separator, and decimal separator.
* @param input
* @param digitPrecision 0 by default
* @param thousandsSeparator comma by default
* @param decimalSeparator period by default
* @param digitPrecision Default is 0.
* @param thousandsSeparator Default is "," (comma).
* @param decimalSeparator Default is "." (period).
*/
export function format_number(
input: string | number,
Expand All @@ -28,12 +29,14 @@ declare namespace estring {
): string;

/**
* Format a time value based on the specified format type
* @param timeValue The time value (number or string).
* @param formatType The format type: 1-12-hour without leading zero + without seconds,
* 2-12-hour with leading zero + without seconds, 3-12-hour with seconds,
* 4-HH:MM:SS format, 5-HH:MM format, 6-MM:SS format
* @param delimiter Optional delimiter string (default is ":").
* Formats a time value based on the provided format type, optionally using a specified delimiter.
* @param timeValue
* @param formatType The format type:
* 1: 12-hour without leading zero + without seconds.
* 2: 12-hour with leading zero + without seconds.
* 3: 12-hour with seconds.
* 4: HH:MM:SS format. 5: HH:MM format. 6: MM:SS format.
* @param delimiter Default is ":" (colon).
*/
export function format_time(
timeValue: number | string,
Expand Down
2 changes: 2 additions & 0 deletions @types/estring-2.0.1.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <library version="2.0.1" src="https://github.com/thinknathan/defold-estring/archive/refs/tags/v2.0.1.zip" />
/// <reference path="./estring-2.0.0.d.ts" />
12 changes: 6 additions & 6 deletions @types/estring.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/** @noSelfInFile */

/**
* eString native extension for concatenation, trimming, splitting, padding, formatting time, and formatting numbers
* Defold native extension with functions for string manipulation in Lua scripts.
* @noResolution
*/
declare namespace estring {
/**
* Trim whitespace from input
* @param input
* Removes leading and trailing whitespace from a string.
* @param input String to trim.
*/
export function trim(input: string): string;

/**
* Create a table with values from `input`, split by `delimiter`
* Splits a string into parts using a specified delimiter and returns them in a table.
* @param input
* @param delimiter
*/
export function split(input: string, delimiter: string): string[];

/**
* Adds `padding` to start of `input` string until it reaches specified `length`
* Pads the start of a string with a specified padding string to reach a target length.
* @param input
* @param padding
* @param length
Expand All @@ -31,7 +31,7 @@ declare namespace estring {
): string;

/**
* Adds `padding` to end of `input` string until it reaches specified `length`
* Pads the end of a string with a specified padding string to reach a target length.
* @param input
* @param padding
* @param length
Expand Down
88 changes: 61 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
# defold-estring

Defold native extension implementing a variety of string-related utilities, including concatenation, trimming, splitting, padding, formatting time, and formatting numbers.
Defold native extension with functions for string manipulation in Lua scripts.

You may prefer [DefString](https://github.com/subsoap/defstring) for a more robust implementation.
You may instead prefer [DefString](https://github.com/subsoap/defstring) for a more robust implementation.

## Functions

- `estring.concat(...)`
- Joins strings or numbers and returns the result.

- `estring.trim(str)`
- Removes leading and trailing whitespace from a string.

- `estring.split(str, delimiter)`
- Splits a string into parts using a specified delimiter and returns them in a table.

- `estring.pad_start(str, padStr, targetLength)`
- Pads the start of a string with a specified padding string to reach a target length.

- `estring.pad_end(str, padStr, targetLength)`
- Pads the end of a string with a specified padding string to reach a target length.

- `estring.format_time(timeValue, formatType, delimiter)`
- Formats a time value based on the provided format type, optionally using a specified delimiter.

- `estring.format_number(number, precision, thousandsSeparator, decimalSeparator)`
- Formats a number as a string with options for precision, thousands separator, and decimal separator.

## Installation

This extension includes types for use with [TypeScript + Defold](https://ts-defold.dev/).

1. Edit game.project
2. Add dependency `https://github.com/thinknathan/defold-estring/archive/main.zip` for the current version
- Or add a specific [release](https://github.com/thinknathan/defold-estring/releases)
- Or add a specific [release](https://github.com/thinknathan/defold-estring/releases)

### TypeScript Definitions

Expand Down Expand Up @@ -47,35 +72,41 @@ npm install git+https://git@github.com/thinknathan/defold-estring.git#^2.0.0 --s
## Usage

```lua
-- Concatenate strings and numbers
local resultConcat = estring.concat("Hello, ", "world", 123, "!")
print(resultConcat) -- Output: Hello, world123!

-- Trim leading and trailing whitespaces
local resultTrim = estring.trim(" This is a string with spaces ")
print(resultTrim) -- Output: This is a string with spaces

-- Split a string
local resultSplit = estring.split("apple,orange,banana", ",")
for i, value in ipairs(resultSplit) do
-- concat
-- Joins strings or numbers and returns the result.
local result_concat = estring.concat("Hello, ", "world", 123, "!")
print(result_concat) -- Output: Hello, world123!

-- trim
-- Removes leading and trailing whitespace from a string.
local result_trim = estring.trim(" This is a string with spaces ")
print(result_trim) -- Output: This is a string with spaces

-- split
-- Splits a string into parts using a specified delimiter and returns them in a table.
local result_split = estring.split("apple,orange,banana", ",")
for i, value in ipairs(result_split) do
print(i, value)
end
-- Output:
-- 1 apple
-- 2 orange
-- 3 banana

-- Pad the start of a string
local resultPadStart = estring.pad_start("42", "0", 5)
print(resultPadStart) -- Output: 00042
-- pad_start
-- Pads the start of a string with a specified padding string to reach a target length.
local result_pad_start = estring.pad_start("42", "0", 5)
print(result_pad_start) -- Output: 00042

-- Pad the end of a string
local resultPadEnd = estring.pad_end("42", "0", 5)
print(resultPadEnd) -- Output: 42000
-- pad_end
-- Pads the end of a string with a specified padding string to reach a target length.
local result_pad_end = estring.pad_end("42", "0", 5)
print(result_pad_end) -- Output: 42000

-- Format time
local resultFormatTime = estring.format_time(os.time(), 2, "-")
print(resultFormatTime) -- Output: 12-45-30 PM
-- format_time
-- Formats a time value based on the provided format type, optionally using a specified delimiter.
local result_format_time = estring.format_time(os.time(), 2, "-")
print(result_format_time) -- Output: 12-45-30 PM
-- Parameters:
-- timeValue: (number or string) The time value to format. It can be either a numeric timestamp or a string representing a date and time.
-- formatType: (integer) Specifies the desired format type:
Expand All @@ -87,9 +118,10 @@ print(resultFormatTime) -- Output: 12-45-30 PM
-- 6: "mm:ss"
-- delimiter: (string, optional) The delimiter used in the formatted time. Default is ":".

-- Format a number
local resultFormatNumber = estring.format_number(1234567.89, 2, ",", ".")
print(resultFormatNumber) -- Output: 1,234,567.89
-- format_number
-- Formats a number as a string with options for precision, thousands separator, and decimal separator.
local result_format_number = estring.format_number(1234567.89, 2, ",", ".")
print(result_format_number) -- Output: 1,234,567.89
-- Parameters:
-- numberValue: (number or string) The number to format.
-- precision: (integer, optional) The number of decimal places. Default is 0.
Expand All @@ -99,7 +131,7 @@ print(resultFormatNumber) -- Output: 1,234,567.89

## Background

This project is an experiment with generating a Defold extension using Chat-GPT. The prompt was as follows:
This project is an experiment with generating a Defold extension using Chat-GPT 3.5. The prompt was as follows:

```
create a defold extension that exposes a module to lua, use c++ that does not use any features newer than 2009, and does not use the standard library, and always uses const char* instead of std::string. the name of the module is estring.
Expand Down Expand Up @@ -142,3 +174,5 @@ adjust the estring_formatNum function so that it can accept either a number or a
```
Add a function that receives a single variable from lua which is either a string or a number, representing a number of seconds, and formats it as hours:minutes:seconds. If hours are zero, remove them. Return the string to lua
```

Further adjustments were made for v2.0.0.
16 changes: 8 additions & 8 deletions estring/api/estring.script_api
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
- name: estring
type: table
desc: Functions for interacting with the eString native extension
desc: "Defold native extension with functions for string manipulation in Lua scripts."
members:
- name: concat
type: function
desc: Joins strings or numbers into a single string
desc: "Joins strings or numbers and returns the result."
returns:
- name: output
type: string
Expand All @@ -16,7 +16,7 @@

- name: trim
type: function
desc: Trim whitespace from input
desc: "Removes leading and trailing whitespace from a string."
returns:
- name: output
type: string
Expand All @@ -26,7 +26,7 @@

- name: split
type: function
desc: Create a table with values from input, split by delimiter
desc: "Splits a string into parts using a specified delimiter and returns them in a table."
returns:
- name: output
type: table
Expand All @@ -38,7 +38,7 @@

- name: pad_start
type: function
desc: Adds padding to start of input string until it reaches specified length
desc: "Pads the start of a string with a specified padding string to reach a target length."
returns:
- name: output
type: string
Expand All @@ -52,7 +52,7 @@

- name: pad_end
type: function
desc: Adds padding to end of input string until it reaches specified length
desc: "Pads the end of a string with a specified padding string to reach a target length."
returns:
- name: output
type: string
Expand All @@ -66,7 +66,7 @@

- name: format_number
type: function
desc: Prettily formats a number, such as `1000` becomes `1,000`
desc: "Formats a time value based on the provided format type, optionally using a specified delimiter."
returns:
- name: output
type: string
Expand All @@ -82,7 +82,7 @@

- name: format_time
type: function
desc: Format a time value based on the specified format type
desc: "Formats a number as a string with options for precision, thousands separator, and decimal separator."
returns:
- name: output
type: string
Expand Down
2 changes: 1 addition & 1 deletion game.project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
title = estring
version = 1.0
version = 2.0.1

[library]
include_dirs = estring
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "@types/defold-estring",
"version": "2.0.0",
"version": "2.0.1",
"description": "Types for Defold extension implementing a variety of string-related utilities",
"license": "CC0-1.0",
"files": ["**/*.d.ts"],
"types": "./@types/estring-2.0.0.d.ts",
"files": [
"**/*.d.ts"
],
"types": "./@types/estring-2.0.1.d.ts",
"main": "./@types/estring",
"scripts": {
"check": "tsc --noEmit",
"prettier": "prettier \"./**.d.ts\" --write"
"prettier": "prettier \"./**/*.{eslintrc,yml,json,ts,md,d.ts}\" --write"
},
"devDependencies": {
"prettier": "^3.0.0",
Expand Down

0 comments on commit 9c021d2

Please sign in to comment.