Skip to content
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

based:0.2.0 #1234

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/preview/based/0.2.0/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2024 Eric Biedert

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions packages/preview/based/0.2.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# based

A package for encoding and decoding in base64, base32, and base16.

## Usage

The package comes with three submodules: `base64`, `base32`, and `base16`. All of them have an `encode` and `decode` function. The package also provides the function aliases

- `encode64` / `decode64`,
- `encode32` / `decode32`, and
- `encode16` / `decode16`.

Both base64 and base32 allow you to choose whether to use padding for encoding via the `pad` parameter, which is enabled by default. Base64 also allows you to encode with the URL-safe alphabet by enabling the `url` parameter, while base32 allows you to encode or decode with the "extended hex" alphabet via the `hex` parameter. Both options are disabled by default. The base16 encoder uses lowercase letters, the decoder is case-insensitive.

You can encode strings, arrays and bytes. The `encode` function will return a string, while the `decode` function will return bytes.

## Example

```typ
#import "@preview/based:0.2.0": base64, base32, base16

#table(
columns: 3,

table.header[*Base64*][*Base32*][*Base16*],

raw(base64.encode("Hello world!")),
raw(base32.encode("Hello world!")),
raw(base16.encode("Hello world!")),

str(base64.decode("SGVsbG8gd29ybGQh")),
str(base32.decode("JBSWY3DPEB3W64TMMQQQ====")),
str(base16.decode("48656C6C6F20776F726C6421"))
)
```

![Result of example code.](./assets/example.svg)
346 changes: 346 additions & 0 deletions packages/preview/based/0.2.0/assets/example.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions packages/preview/based/0.2.0/assets/example.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#import "@preview/based:0.2.0": base64, base32, base16

#set text(size: 14pt)
#set page(
width: auto,
height: auto,
margin: 1em,
fill: none,
background: pad(0.5pt, box(
width: 100%,
height: 100%,
radius: 4pt,
fill: white,
stroke: white.darken(10%),
)),
)

#table(
columns: 3,
inset: 0.5em,

table.header[*Base64*][*Base32*][*Base16*],

raw(base64.encode("Hello world!")),
raw(base32.encode("Hello world!")),
raw(base16.encode("Hello world!")),

str(base64.decode("SGVsbG8gd29ybGQh")),
str(base32.decode("JBSWY3DPEB3W64TMMQQQ====")),
str(base16.decode("48656C6C6F20776F726C6421"))
)
21 changes: 21 additions & 0 deletions packages/preview/based/0.2.0/src/base16.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#let plugin = plugin("based.wasm")

/// Encodes the given data as a hex string.
///
/// Arguments:
/// - data: The data to encode. Must be of type array, bytes, or string.
///
/// Returns: The encoded string (lowercase).
#let encode(data) = {
str(plugin.encode16(bytes(data)))
}

/// Decodes the given hex string.
///
/// Arguments:
/// - string: The string to decode (case-insensitive).
///
/// Returns: The decoded bytes.
#let decode(string) = {
plugin.decode16(bytes(string))
}
27 changes: 27 additions & 0 deletions packages/preview/based/0.2.0/src/base32.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#let plugin = plugin("based.wasm")

/// Encodes the given data in base32 format.
///
/// Arguments:
/// - data: The data to encode. Must be of type array, bytes, or string.
/// - pad: Whether to pad the output with "=" characters.
/// - hex: Whether to use the extended base32hex alphabet.
///
/// Returns: The encoded string.
#let encode(data, pad: true, hex: false) = {
let flags = bytes((if pad { 1 } else { 0 }, if hex { 1 } else { 0 }))
str(plugin.encode32(bytes(data), flags))
}

/// Decodes the given base32 string.
///
/// Arguments:
/// - string: The string to decode.
/// - hex: Whether to use the extended base32hex alphabet.
///
/// Returns: The decoded bytes.
#let decode(string, hex: false) = {
let flags = bytes((0, if hex { 1 } else { 0 }))
let string = string.trim("=", at: end)
plugin.decode32(bytes(string), flags)
}
29 changes: 29 additions & 0 deletions packages/preview/based/0.2.0/src/base64.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#let plugin = plugin("based.wasm")

/// Encodes the given data in base64 format.
///
/// Arguments:
/// - data: The data to encode. Must be of type array, bytes, or string.
/// - pad: Whether to pad the output with "=" characters.
/// - url: Whether to use the URL safe alphabet.
///
/// Returns: The encoded string.
#let encode(data, pad: true, url: false) = {
let flags = bytes((if pad { 1 } else { 0 }, if url { 1 } else { 0 }))
str(plugin.encode64(bytes(data), flags))
}

/// Decodes the given base64 string.
///
/// URL safe characters are automatically converted to their standard
/// counterparts.
///
/// Arguments:
/// - string: The string to decode.
///
/// Returns: The decoded bytes.
#let decode(string) = {
let flags = bytes((0, 0))
let string = string.replace("-", "+").replace("_", "/").trim("=", at: end)
plugin.decode64(bytes(string), flags)
}
Binary file added packages/preview/based/0.2.0/src/based.wasm
Binary file not shown.
12 changes: 12 additions & 0 deletions packages/preview/based/0.2.0/src/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#import "base64.typ"
#import "base32.typ"
#import "base16.typ"

#let encode64 = base64.encode
#let decode64 = base64.decode

#let encode32 = base32.encode
#let decode32 = base32.decode

#let encode16 = base16.encode
#let decode16 = base16.decode
10 changes: 10 additions & 0 deletions packages/preview/based/0.2.0/typst.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "based"
version = "0.2.0"
entrypoint = "src/lib.typ"
authors = ["Eric Biedert"]
license = "MIT"
description = "Encoder and decoder for base64, base32, and base16."
repository = "https://github.com/EpicEricEE/typst-based"
exclude = ["assets", "tests"]
categories = ["scripting"]
Loading