Skip to content

Commit 2a99938

Browse files
committed
[tfjs-core] helper functions for base64
1 parent bff4539 commit 2a99938

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

tfjs-core/src/io/io_utils.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2018 Google LLC. All Rights Reserved.
3+
* Copyright 2019 Google LLC. All Rights Reserved.
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
@@ -335,3 +335,21 @@ export function getModelArtifactsInfoForJSON(modelArtifacts: ModelArtifacts):
335335
modelArtifacts.weightData.byteLength,
336336
};
337337
}
338+
339+
/**
340+
* Make Base64 string URL safe by replacing `+` with `-` and `/` with `_`.
341+
*
342+
* @param str Base64 string to make URL safe.
343+
*/
344+
export function urlSafeBase64(str: string): string {
345+
return str.replace(/\+/g, '-').replace(/\//g, '_');
346+
}
347+
348+
/**
349+
* Revert Base64 URL safe changes by replacing `-` with `+` and `_` with `/`.
350+
*
351+
* @param str URL safe Base string to revert changes.
352+
*/
353+
export function urlUnsafeBase64(str: string): string {
354+
return str.replace(/-/g, '+').replace(/_/g, '/');
355+
}

0 commit comments

Comments
 (0)