@gkucmierz/utils 1.14.0 → 1.16.0

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/main.mjs CHANGED
@@ -1,4 +1,7 @@
1
1
 
2
+ import {
3
+ fromBase64, toBase64
4
+ } from './src/base64.mjs'
2
5
  import {
3
6
  bijective2num, bijective2numBI, num2bijective, num2bijectiveBI
4
7
  } from './src/bijective-numeration.mjs'
@@ -39,6 +42,7 @@ import {
39
42
  tonelliShanksBI
40
43
  } from './src/tonelli-shanks.mjs'
41
44
 
45
+ export * from './src/base64.mjs';
42
46
  export * from './src/bijective-numeration.mjs';
43
47
  export * from './src/egcd.mjs';
44
48
  export * from './src/factors.mjs';
@@ -54,5 +58,5 @@ export * from './src/square-root.mjs';
54
58
  export * from './src/tonelli-shanks.mjs';
55
59
 
56
60
  export default [
57
- bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, heronsFormula, heronsFormulaBI, lcm, lcmBI, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
61
+ fromBase64, toBase64, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, heronsFormula, heronsFormulaBI, lcm, lcmBI, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
58
62
  ];
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.14.0",
3
+ "version": "1.16.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@gkucmierz/utils",
9
- "version": "1.14.0",
9
+ "version": "1.16.0",
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.14.0",
3
+ "version": "1.16.0",
4
4
  "description": "Usefull functions for solving programming tasks",
5
5
  "main": "main.mjs",
6
6
  "scripts": {
@@ -0,0 +1,25 @@
1
+
2
+ import {
3
+ toBase64,
4
+ fromBase64,
5
+ toBase64Url,
6
+ fromBase64Url,
7
+ } from '../src/base64.mjs';
8
+
9
+ describe('base64', () => {
10
+ it('toBase64', () => {
11
+ expect(toBase64('🥚')).toBe('Ptha3Q==');
12
+ expect(toBase64('🐔')).toBe('PdgU3A==');
13
+ });
14
+
15
+ it('fromBase64', () => {
16
+ expect(fromBase64('Ptha3Q==')).toBe('🥚');
17
+ expect(fromBase64('PdgU3A==')).toBe('🐔');
18
+ });
19
+
20
+ // it('toBase64Url', () => {
21
+ // });
22
+
23
+ // it('fromBase64', () => {
24
+ // });
25
+ });
package/src/base64.mjs ADDED
@@ -0,0 +1,33 @@
1
+
2
+ export const toBase64 = string => {
3
+ const codeUnits = new Uint16Array(string.length);
4
+ for (let i = 0; i < codeUnits.length; i++) {
5
+ codeUnits[i] = string.charCodeAt(i);
6
+ }
7
+ return btoa(String.fromCharCode(...new Uint8Array(codeUnits.buffer)));
8
+ };
9
+
10
+ export const fromBase64 = encoded => {
11
+ const binary = atob(encoded);
12
+ const bytes = new Uint8Array(binary.length);
13
+ for (let i = 0; i < bytes.length; i++) {
14
+ bytes[i] = binary.charCodeAt(i);
15
+ }
16
+ return String.fromCharCode(...new Uint16Array(bytes.buffer));
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
+ };