Skip to content

Commit

Permalink
feat: add isStringNumeric to library (#28)
Browse files Browse the repository at this point in the history
Signed-off-by: Mauro Stettler <[email protected]>
Co-authored-by: Jeroen Op 't Eynde <[email protected]>
  • Loading branch information
replay and Duologic authored Mar 4, 2024
1 parent 295816a commit fc2e57a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ascii.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
[d.arg('c', d.T.string)]
),
isNumber(c): std.isNumber(c) || (cp(c) >= 48 && cp(c) < 58),

'#isStringNumeric':: d.fn(
'`isStringNumeric` reports whether string `s` consists only of numeric characters.',
[d.arg('str', d.T.string)]
),
isStringNumeric(str): std.all(std.map(self.isNumber, std.stringChars(str))),
}
9 changes: 9 additions & 0 deletions docs/ascii.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local ascii = import "github.com/jsonnet-libs/xtd/ascii.libsonnet"

* [`fn isLower(c)`](#fn-islower)
* [`fn isNumber(c)`](#fn-isnumber)
* [`fn isStringNumeric(str)`](#fn-isstringnumeric)
* [`fn isUpper(c)`](#fn-isupper)

## Fields
Expand All @@ -34,6 +35,14 @@ isNumber(c)

`isNumber` reports whether character `c` is a number.

### fn isStringNumeric

```ts
isStringNumeric(str)
```

`isStringNumeric` reports whether string `s` consists only of numeric characters.

### fn isUpper

```ts
Expand Down
44 changes: 44 additions & 0 deletions test/ascii_test.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local ascii = import '../ascii.libsonnet';
local test = import 'github.com/jsonnet-libs/testonnet/main.libsonnet';

test.new(std.thisFile)

+ test.case.new(
name='all numeric',
test=test.expect.eq(
actual=ascii.isStringNumeric('123'),
expected=true,
)
)

+ test.case.new(
name='only beginning numeric',
test=test.expect.eq(
actual=ascii.isStringNumeric('123abc'),
expected=false,
)
)

+ test.case.new(
name='only end numeric',
test=test.expect.eq(
actual=ascii.isStringNumeric('abc123'),
expected=false,
)
)

+ test.case.new(
name='none numeric',
test=test.expect.eq(
actual=ascii.isStringNumeric('abc'),
expected=false,
)
)

+ test.case.new(
name='empty',
test=test.expect.eq(
actual=ascii.isStringNumeric(''),
expected=true,
)
)

0 comments on commit fc2e57a

Please sign in to comment.