@gkucmierz/utils 1.15.0 → 1.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.15.0",
3
+ "version": "1.16.1",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@gkucmierz/utils",
9
- "version": "1.15.0",
9
+ "version": "1.16.1",
10
10
  "license": "MIT",
11
11
  "devDependencies": {
12
12
  "husky": "^8.0.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.15.0",
3
+ "version": "1.16.1",
4
4
  "description": "Usefull functions for solving programming tasks",
5
5
  "main": "main.mjs",
6
6
  "scripts": {
@@ -2,16 +2,28 @@
2
2
  import {
3
3
  toBase64,
4
4
  fromBase64,
5
+ toBase64Url,
6
+ fromBase64Url,
5
7
  } from '../src/base64.mjs';
6
8
 
7
9
  describe('base64', () => {
8
10
  it('toBase64', () => {
9
11
  expect(toBase64('🥚')).toBe('Ptha3Q==');
10
12
  expect(toBase64('🐔')).toBe('PdgU3A==');
13
+ expect(toBase64('')).toBe('++8=');
11
14
  });
12
15
 
13
16
  it('fromBase64', () => {
14
17
  expect(fromBase64('Ptha3Q==')).toBe('🥚');
15
18
  expect(fromBase64('PdgU3A==')).toBe('🐔');
19
+ expect(fromBase64('++8=')).toBe('');
20
+ });
21
+
22
+ it('toBase64Url', () => {
23
+ expect(toBase64Url('')).toBe('--8=');
24
+ });
25
+
26
+ it('fromBase64', () => {
27
+ expect(fromBase64Url('--8=')).toBe('');
16
28
  });
17
29
  });
package/src/base64.mjs CHANGED
@@ -15,3 +15,19 @@ export const fromBase64 = encoded => {
15
15
  }
16
16
  return String.fromCharCode(...new Uint16Array(bytes.buffer));
17
17
  };
18
+
19
+ const toUrl = {
20
+ '+': '-',
21
+ '/': '_',
22
+ };
23
+ export const toBase64Url = string => {
24
+ return toBase64(string).replace(/[\+\/]/g, c => toUrl[c]);
25
+ };
26
+
27
+ const fromUrl = {
28
+ '-': '+',
29
+ '_': '/',
30
+ };
31
+ export const fromBase64Url = encoded => {
32
+ return fromBase64(encoded.replace(/[\-\_]/g, c => fromUrl[c]));
33
+ };