-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More formatting options #21
Open
Xenapte
wants to merge
5
commits into
MeteorDevelopment:master
Choose a base branch
from
Xenapte:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0fa88be
Add `format` and `formatDateTime`
Xenapte 3cbaafd
Merge branch 'MeteorDevelopment:master' into master
Xenapte df7b249
fix null formatting
Xenapte f4251b6
add option to specify timezone
Xenapte 07567ba
style improvements
Xenapte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,11 @@ | |
import org.meteordev.starscript.value.Value; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.IllegalFormatException; | ||
import java.util.Random; | ||
import java.util.TimeZone; | ||
|
||
/** Standard library with some default functions and variables. */ | ||
public class StandardLib { | ||
|
@@ -35,6 +38,10 @@ public static void init(Starscript ss) { | |
ss.set("contains", StandardLib::contains); | ||
ss.set("replace", StandardLib::replace); | ||
ss.set("pad", StandardLib::pad); | ||
|
||
// Formatters | ||
ss.set("formatDateTime", StandardLib::formatDateTime); | ||
ss.set("format", StandardLib::format); | ||
} | ||
|
||
// Numbers | ||
|
@@ -166,4 +173,47 @@ public static Value pad(Starscript ss, int argCount) { | |
|
||
return Value.string(new String(padded)); | ||
} | ||
|
||
public static Value formatDateTime(Starscript ss, int argCount) { | ||
if (argCount < 1 || argCount > 2) ss.error("formatTime(fmt, timezone) requires 1 to 2 arguments, got %d.", argCount); | ||
try { | ||
String timeZone = null; | ||
if (argCount == 2) timeZone = ss.popString("Argument to formatTime(fmt, timezone) needs to be a string."); | ||
String fmt = ss.popString("Argument to formatTime(fmt, timezone) needs to be a string."); | ||
SimpleDateFormat formatter = new SimpleDateFormat(fmt); | ||
if (timeZone != null && !timeZone.isEmpty()) formatter.setTimeZone(TimeZone.getTimeZone(timeZone)); | ||
return Value.string(formatter.format(new Date())); | ||
} | ||
catch (IllegalArgumentException e) { | ||
ss.error(e.toString()); | ||
} | ||
return Value.null_(); | ||
} | ||
|
||
public static Value format(Starscript ss, int argCount) { | ||
if (argCount < 1) ss.error("format(fmt, ...args) requires at least 1 argument, got %d.", argCount); | ||
ArrayList<Object> args = new ArrayList<Object>(); | ||
for (int i = 1; i < argCount; i ++) { | ||
Value v = ss.pop(); | ||
Object o = null; | ||
switch (v.type) { | ||
case Boolean: o = v.getBool(); break; | ||
case Number: o = v.getNumber(); break; | ||
case String: o = v.getString(); break; | ||
case Function: o = v.getFunction(); break; | ||
case Map: o = v.getMap(); break; | ||
case Null: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of initializing |
||
default: break; | ||
} | ||
args.add(0, o); | ||
} | ||
String fmt = ss.popString("Argument `fmt` to format() needs to be a string."); | ||
try { | ||
return Value.string(String.format(fmt, args.toArray())); | ||
} | ||
catch (IllegalFormatException e) { | ||
ss.error(e.toString()); | ||
} | ||
return Value.null_(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the current implementation is less than ideal in performance
ArrayList
constructor means this method will potentially cause several object arrays to be allocated as the list will have to be resizedit would be better to just use an object array directly and filling it with a reverse for i loop