@ankarhem/secrecy-ts
TypeScript icon, indicating that this package has built-in type declarations

0.1.7 • Public • Published

Coverage Status

secrecy-ts

A typescript port of the rust library secrecy.

Usage

const secret = new Secret("password");

console.log(secret.expose()); // "password"

// If not explicitly exposed, the secret is hidden
console.log(secret); // "[REDACTED]"
console.log(String(secret)); // "[REDACTED]"
console.log(secret.toString()); // "[REDACTED]"
console.log(secret.valueOf().toString()); // "[REDACTED]"
console.log(`${secret}`); // "[REDACTED]"
console.log(JSON.stringify(secret)); // "[REDACTED]"
console.log(JSON.stringify({ secret: secret })); // "{"secret":"[REDACTED]"}"

// Compare secrets with other secrets or with non-secret values
console.log(secret.equals(secret)); // true
console.log(secret.equals("not a secret")); // false

// Clone secrets
const cloned = secret.clone();
console.log(cloned.expose()); // "password"
console.log(Object.is(secret, cloned)); // false
console.log(secret.equals(cloned)); // true

// Map the secret value to a new secret value
const mapped = secret.map((value) => value.length);
console.log(mapped.expose()); // 8

// Assert that a value is a secret
assertSecret(secret);
// If the value is not a secret, an error is thrown
assertSecret("not a secret"); // throws SecretError

// Check if a value is a secret
const something = new Secret("something") as unknown;
if (isSecret(something)) {
  console.log(something.expose()); // "something"
}

Readme

Keywords

none

Package Sidebar

Install

npm i @ankarhem/secrecy-ts

Weekly Downloads

3

Version

0.1.7

License

MIT

Unpacked Size

45.9 kB

Total Files

82

Last publish

Collaborators

  • jakobankarhem