Skip to content

Commit

Permalink
docs: updated readme
Browse files Browse the repository at this point in the history
- 100% coverage
  • Loading branch information
cecilia-sanare committed Jan 14, 2024
1 parent 26859a9 commit 3819b86
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .swcrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"sourceMaps": true,
"sourceMaps": "inline",
"jsc": {
"target": "es2022",
"parser": {
"syntax": "typescript",
"tsx": true
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ export function MySimpleInput({ className: externalClassName, value: externalVal
}
```

## `useSubtleCrypto`

```tsx
import { useSubtleCrypto } from '@rain-cafe/react-utils';

export type ProfileProps = {
email?: string;
};

export function Profile({ email }: ProfileProps) {
const hashedEmail = useSubtleCrypto('SHA-256', email);

return <img src={`https://gravatar.com/avatar/${hashedEmail}.jpg`} />;
}
```

[_**Want to Contribute?**_](/CONTRIBUTING.md)

[npm-version-image]: https://img.shields.io/npm/v/@rain-cafe/react-utils.svg
Expand Down
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Config } from 'jest';
import { readFileSync } from 'fs';

// Changed sourcemaps to inline resolving issues with https://github.com/swc-project/swc/issues/3854
const config = JSON.parse(readFileSync(`${__dirname}/.swcrc`, 'utf-8'));

export default {
Expand All @@ -11,9 +12,8 @@ export default {
'^.+\\.(t|j)sx?$': ['@swc/jest', config],
},

setupFiles: ['@inrupt/jest-jsdom-polyfills'],
collectCoverageFrom: ['<rootDir>/src/**/*'],
coveragePathIgnorePatterns: ['__tests__'],
setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'],
setupFilesAfterEnv: ['@inrupt/jest-jsdom-polyfills', '@testing-library/jest-dom/extend-expect'],
extensionsToTreatAsEsm: ['.ts', '.tsx'],
} satisfies Config;
4 changes: 1 addition & 3 deletions src/hooks/use-subtle-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export function useSubtleCrypto(algorithm: Algorithms, value?: string | null): s
const [hashedValue, setHashedValue] = useState<string>();

useEffect(() => {
if (value) {
hash(algorithm, value).then(setHashedValue);
}
hash(algorithm, value).then(setHashedValue);
}, [algorithm, value]);

return hashedValue;
Expand Down
8 changes: 8 additions & 0 deletions src/utils/__tests__/subtle-crypto.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,13 @@ describe('Crypto Utils', () => {
])('should support %s', async (algorithm: Algorithms, expectedValue: string) => {
await expect(hash(algorithm, 'test')).resolves.toEqual(expectedValue);
});

it('should support being provided null', async () => {
await expect(hash('SHA-256', null)).resolves.toEqual(null);
});

it('should support being provided undefined', async () => {
await expect(hash('SHA-256', undefined)).resolves.toEqual(null);
});
});
});
4 changes: 3 additions & 1 deletion src/utils/subtle-crypto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export type Algorithms = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512';

export async function hash(algorithm: Algorithms, message: string): Promise<string> {
export async function hash(algorithm: Algorithms, message?: string): Promise<string> {
if (!message) return null;

const msgBuffer = new TextEncoder().encode(message);

// hash the message
Expand Down

0 comments on commit 3819b86

Please sign in to comment.