Skip to content

Commit

Permalink
Merge branch 'release/v0.26.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Nov 7, 2024
2 parents 834990a + e18c3d9 commit c30c05f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.25.11",
"version": "0.26.0",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
27 changes: 9 additions & 18 deletions src/common/data/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable no-control-regex */
/* eslint-disable prefer-spread */

import { base64, base64nopad, base64urlnopad } from './basex-secure'
import { base64, base64nopad, base64urlnopad, str } from './basex-secure'
import { jsonStringifySafe } from './json'

export type BinInput = Uint8Array | ArrayBuffer | string | number[]
Expand Down Expand Up @@ -110,30 +110,21 @@ export function toBase64(bin: BinInput, stripPadding = false): string {
let sb = ''
if (typeof Buffer !== 'undefined') {
sb = Buffer.from(bytes).toString('base64')
} else if (typeof btoa !== 'undefined') {
let s = ''
for (let i = 0; i < bytes.byteLength; i++)
s += String.fromCharCode(bytes[i])
sb = btoa(s)
} else {
if (stripPadding)
return sb.replace(/\=/g, '')
return sb
}
if (stripPadding) {
return base64nopad.encode(bytes)
}
if (stripPadding)
return sb.replace(/\=/g, '')
return sb
return base64.encode(bytes)
}

/** Converts `+` -> `-` and `/` -> `_`. Always strips `=` */
export function toBase64Url(bin: BinInput): string {
const bytes = toUint8Array(bin)
if (typeof Buffer !== 'undefined')
return Buffer.from(bytes).toString('base64url').replace(/\=/g, '')
if (typeof btoa === 'undefined') {
let s = ''
for (let i = 0; i < bytes.byteLength; i++)
s += String.fromCharCode(bytes[i])
return btoa(s).replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '')
}
return Buffer.from(bytes).toString('base64url').replace(/\=/g, '')
return base64urlnopad.encode(bytes)
}

Expand All @@ -144,7 +135,7 @@ export function fromBase64(s: string): Uint8Array {
const buf = Buffer.from(s, 'base64')
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)
}
if (typeof atob === 'undefined') {
if (typeof atob !== 'undefined') {
const a = atob(s)
const bytes = new Uint8Array(a.length)
for (let i = 0; i < a.length; i++)
Expand Down

0 comments on commit c30c05f

Please sign in to comment.