Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pioner921227 committed Jan 17, 2025
1 parent 2838bb0 commit eac42d2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 33 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ npm install react-native-xxhash


```js
import { multiply } from 'react-native-xxhash';
import { hash128, hash64 } from 'react-native-xxhash';

// ...

const result = await multiply(3, 7);
//This function provides a fast and deterministic 128-bit hash for a given string input.
const result_hash_128_bits = hash128("hello world");

//This function provides a fast and deterministic 64-bit hash for a given string input.
const result_hash_64_bits = hash64("hello world");
```


Expand Down
6 changes: 3 additions & 3 deletions cpp/react-native-xxhash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void xxhash::install(jsi::Runtime* rt_ptr) {
});

jsi::Function hash64 = jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, "xxhash128"), 1,
runtime, jsi::PropNameID::forAscii(runtime, "hash64"), 1,
[](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args,
size_t count) {
const jsi::Value& arg = args[0];
Expand All @@ -37,7 +37,7 @@ void xxhash::install(jsi::Runtime* rt_ptr) {
return jsi::String::createFromUtf8(rt, ss.str());
});

runtime.global().setProperty(runtime, "xxhash128", std::move(hash128));
runtime.global().setProperty(runtime, "__xxhash128", std::move(hash128));

runtime.global().setProperty(runtime, "xxhash64", std::move(hash64));
runtime.global().setProperty(runtime, "__xxhash64", std::move(hash64));
}
10 changes: 3 additions & 7 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { useState, useEffect } from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { multiply } from 'react-native-xxhash';
import { StyleSheet, Text, View } from 'react-native';
import { hash64,hash128 } from 'react-native-xxhash';

export default function App() {
const [result, setResult] = useState<number | undefined>();

useEffect(() => {
multiply(3, 7).then(setResult);
}, []);
const result = hash128("hello world");

return (
<View style={styles.container}>
Expand Down
84 changes: 63 additions & 21 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,64 @@
import { NativeModules, Platform } from 'react-native';

const LINKING_ERROR =
`The package 'react-native-xxhash' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo Go\n';

const Xxhash = NativeModules.Xxhash
? NativeModules.Xxhash
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);

export function multiply(a: number, b: number): Promise<number> {
return Xxhash.multiply(a, b);

declare global {
var __xxhash128: (input: string) => string;
var __xxhash64: (input: string) => string;
}

/**
* Hashes the input string using the xxhash128 algorithm.
* This function provides a fast and deterministic 128-bit hash for a given string input.
*
* @param {string} input - The string to hash.
* @returns {string} The hashed string as a hexadecimal representation of the 128-bit hash.
* @throws {Error} If the input is not provided.
* @throws {Error} If the input is not of type string.
*
* @example
* const result = hash128("hello world");
* console.log(result); // Example output: "3a2b9e6a2b5e7e5a9e6a2b5e7e5a2a2"
*
* @description
* This function uses the xxhash128 algorithm, which provides a larger hash size (128-bit) compared to xxhash64.
* It is ideal for scenarios where a reduced collision risk is critical, such as in distributed systems or when hashing larger datasets.
*/
export const hash128 = (input: string): string => {
if(!input){
throw new Error('Input is required');
}

if(typeof input !== 'string'){
throw new Error('Input must be a string');
}

return globalThis.__xxhash128(input);
}

/**
* Hashes the input string using the xxhash64 algorithm.
* This function provides a fast and deterministic 64-bit hash for a given string input.
*
* @param {string} input - The string to hash.
* @returns {string} The hashed string as a hexadecimal representation of the 64-bit hash.
* @throws {Error} If the input is not provided.
* @throws {Error} If the input is not of type string.
*
* @example
* const result = hash64("hello world");
* console.log(result); // Example output: "9e6a2b5e7e5a2a2e"
*
* @description
* This function uses the xxhash64 algorithm, known for its high performance and low collision rate.
* It is ideal for hashing small to medium-sized strings.
*/

export const hash64 = (input: string): string => {
if(!input){
throw new Error('Input is required');
}

if(typeof input !== 'string'){
throw new Error('Input must be a string');
}

return globalThis.__xxhash64(input);
}

0 comments on commit eac42d2

Please sign in to comment.