Skip to content

Commit

Permalink
Merge pull request #31 from thutasann/feat/number
Browse files Browse the repository at this point in the history
add: generate password
  • Loading branch information
thutasann authored Nov 10, 2024
2 parents 1a98a51 + 0ccf9cd commit b73df7a
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 21 deletions.
File renamed without changes.
11 changes: 6 additions & 5 deletions __test__/benchmark/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { array_benchmark_test } = require('./array')
const { leetcode_benchmark_test } = require('./leetcode')
const { number_benchmark_test } = require('./number')
const { sort_benchmark_test } = require('./sort')
const { string_benchmark_test } = require('./string')
// @ts-check
const { array_benchmark_test } = require('./array.benchmark')
const { leetcode_benchmark_test } = require('./leetcode.benchmark')
const { number_benchmark_test } = require('./number.benchmark')
const { sort_benchmark_test } = require('./sort.benchmark')
const { string_benchmark_test } = require('./string.benchmark')

/** benchmark main fn */
;(async function main() {
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions __test__/benchmark/results/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

| Method | Time (seconds) |
| ----------------------------------- | -------------- |
| Nexium chunkArray | 0.068 |
| JavaScript chunkArray | 0.001 |
| Nexium chunkArray | 0.069 |
| JavaScript chunkArray | 0.006 |
| - | - |
| Nexium uniqueArray Number | 0.004 |
| JavaScript uniqueArray Number | 0.005 |
| Nexium uniqueArray Number | 0.005 |
| JavaScript uniqueArray Number | 0.002 |
| - | - |
| Nexium uniqueArray ArrObj | 0.020 |
| JavaScript uniqueArray ArrObj | 0.033 |
| JavaScript uniqueArray ArrObj | 0.034 |
| - | - |
| Nexium countNonRepeating Number | 0.001 |
| JavaScript countNonRepeating Number | 0.002 |
| JavaScript countNonRepeating Number | 0.003 |
| - | - |
2 changes: 1 addition & 1 deletion __test__/benchmark/results/leetcodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| JavaScript Two Sum Number | 0.000 |
| - | - |
| Nexium Longest Substring | 0.000 |
| JavaScript Longest Substring | 0.001 |
| JavaScript Longest Substring | 0.002 |
| - | - |
| Nexium Valid Parentheses | 0.000 |
| JavaScript Valid Parentheses | 0.001 |
Expand Down
6 changes: 3 additions & 3 deletions __test__/benchmark/results/sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

| Method | Time (seconds) |
| ----------------------------- | -------------- |
| Nexium Bubble Sort Number | 0.079 |
| Nexium Bubble Sort Number | 0.080 |
| JavaScript Bubble Sort Number | 0.006 |
| - | - |
| Nexium Quick Sort Number | 0.089 |
| JavaScript Quick Sort Number | 2.455 |
| Nexium Quick Sort Number | 0.079 |
| JavaScript Quick Sort Number | 2.030 |
15 changes: 9 additions & 6 deletions __test__/benchmark/results/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

| Method | Time (seconds) |
| -------------------------- | -------------- |
| Nexium UUID generation | 0.000367 |
| JavaScript UUID generation | 0.001530 |
| Nexium UUID generation | 0.000336 |
| JavaScript UUID generation | 0.001804 |
| - | - |
| Nexium Slugify | 0.000184 |
| Nexium Slugify | 0.000182 |
| JavaScript Slugify | 0.000177 |
| - | - |
| Nexium Reverse | 0.000097 |
| JavaScript Reverse | 0.000211 |
| Nexium Reverse | 0.000098 |
| JavaScript Reverse | 0.000210 |
| - | - |
| Nexium Palindrome | 0.000082 |
| Nexium Palindrome | 0.000083 |
| JavaScript Palindrome | 0.000185 |
| - | - |
| Nexium Password Time | 0.000181 |
| JavaScript Password Time | 0.000242 |
| - | - |
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
benchmark,
benchmark_args,
isPalindrome,
generatePassword,
} = require('./utils/index')

/** iterations */
Expand Down Expand Up @@ -48,6 +49,13 @@ async function string_benchmark_test() {
results.push({ Method: 'JavaScript Palindrome', Time: jsPalindromeTime.toFixed(6) })
results.push({})

// ----------- Palindrome String Benchmarks
const nPasswordTime = benchmark_args(() => NString.generatePassword(12), [], iterations)
const jsPasswordTime = benchmark_args(() => generatePassword(12), [], iterations)
results.push({ Method: 'Nexium Password Time', Time: nPasswordTime.toFixed(6) })
results.push({ Method: 'JavaScript Password Time', Time: jsPasswordTime.toFixed(6) })
results.push({})

await updateResult(results, './results/string.md', 'String Benchmark')
}

Expand Down
17 changes: 17 additions & 0 deletions __test__/benchmark/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ function quickSort(array) {
return [...quickSort(left), pivot, ...quickSort(right)]
}

/**
* Generate password
* @param {number} length
*/
function generatePassword(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+~`|}{[]:;?><,./-='
let password = ''

for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length)
password += characters[randomIndex]
}

return password
}

module.exports = {
generateUUIDVanilla,
random_users,
Expand All @@ -271,4 +287,5 @@ module.exports = {
countNonRepeatingElements,
bubbleSort,
quickSort,
generatePassword,
}
21 changes: 21 additions & 0 deletions __test__/string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,24 @@ describe('String Functions', () => {
expect(alphanumeric.test(randomString)).toBe(true)
})
})

describe('Password Generator', () => {
test('generates password of specified length', () => {
const length = 12
const password = NString.generatePassword(length)
expect(password.length).toBe(length)
})

test('handles minimum length', () => {
const password = NString.generatePassword(1)
expect(password.length).toBe(1)
})

test('generates passwords containing alphanumeric and special characters', () => {
const password = NString.generatePassword(15)
expect(/[A-Z]/.test(password)).toBe(true) // Uppercase letters
expect(/[a-z]/.test(password)).toBe(true) // Lowercase letters
expect(/[0-9]/.test(password)).toBe(true) // Digits
expect(/[!@#$%^&*()_\-+=<>?]/.test(password)).toBe(true) // Special characters
})
})
10 changes: 10 additions & 0 deletions docs/content/docs/strings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,13 @@ Counts how many times a substring appears in a string.
```js
const countOccurrences = NString.countOccurrences('hello world hello', 'hello')
```

---

## generatePassword

generate random password with the given length

```js
const password1 = generatePassword(12)
```
1 change: 1 addition & 0 deletions src/addon/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ napi_value EndsWith(napi_env env, napi_callback_info info);
napi_value ReplaceString(napi_env env, napi_callback_info info);
napi_value ReplaceDiacritics(napi_env env, napi_callback_info info);
napi_value GenerateRandomString(napi_env env, napi_callback_info info);
napi_value GeneratePassword(napi_env env, napi_callback_info info);

// ------ UUID Functions

Expand Down
26 changes: 26 additions & 0 deletions src/addon/shared/string_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include <string.h>
#include <time.h>

// Character set for password generation
const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"!@#$%^&*()_-+=<>?";

/** Helper function to check if a string a palindrome */
bool is_palindrome(const char *str, size_t length) {
size_t start = 0;
Expand Down Expand Up @@ -279,3 +285,23 @@ char *generate_random_string(int length, const char *pattern) {
result[length] = '\0';
return result;
}

/** Helper function to generate password */
char *generatePassword(int length) {
// seed the random number genrator
srand((unsigned int)time(NULL));

// allocate memory for the password
char *password = (char *)malloc((length + 1) * sizeof(char));

if (!password)
return NULL;

for (int i = 0; i < length; i++) {
int index = rand() % (sizeof(charset) - 1);
password[i] = charset[index];
}
password[length] = '\0';

return password;
}
4 changes: 4 additions & 0 deletions src/addon/string_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ void InitStringFunctions(napi_env env, napi_value exports) {
napi_value generateRandomStringFn;
napi_create_function(env, NULL, 0, GenerateRandomString, NULL, &generateRandomStringFn);
napi_set_named_property(env, exports, "generateRandomString", generateRandomStringFn);

napi_value generatePassword_fn;
napi_create_function(env, NULL, 0, GeneratePassword, NULL, &generatePassword_fn);
napi_set_named_property(env, exports, "generatePassword", generatePassword_fn);
}
31 changes: 31 additions & 0 deletions src/addon/utils/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void to_kebab_case(const char *input, char *output);
char *replace_string(const char *str, const char *pattern, const char *replacement);
char *replace_diacritics(const char *str);
char *generate_random_string(int length, const char *pattern);
char *generatePassword(int length);

/** Trim Start Function */
napi_value TrimStart(napi_env env, napi_callback_info info) {
Expand Down Expand Up @@ -537,5 +538,35 @@ napi_value GenerateRandomString(napi_env env, napi_callback_info info) {
napi_create_string_utf8(env, result_str, NAPI_AUTO_LENGTH, &result);
free(result_str);

return result;
}

/** Function to generate a password */
napi_value GeneratePassword(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
napi_status status;

status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
if (status != napi_ok || argc < 1)
return NULL;

int32_t length;
status = napi_get_value_int32(env, args[0], &length);
if (status != napi_ok || length <= 0)
return NULL;

char *password = generatePassword(length);
if (!password)
return NULL;

napi_value result;
status = napi_create_string_utf8(env, password, NAPI_AUTO_LENGTH, &result);

free(password);

if (status != napi_ok)
return NULL;

return result;
}
1 change: 1 addition & 0 deletions src/typescript/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ declare module '*.node' {
export function replaceString(str: string, target: string, replacement: string): string
export function replaceDiacritics(str: string): string
export function generateRandomString(length: number, pattern: string): string
export function generatePassword(length: number): string

// ----------- UUID Functions
export function generateUUID(): string
Expand Down
12 changes: 12 additions & 0 deletions src/typescript/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
replaceString as replaceStringFn,
replaceDiacritics as replaceDiacriticsFn,
generateRandomString as generateRandomStringFn,
generatePassword as generatePasswordFn,
} from '../../build/Release/nexium.node'

/** String Methods */
Expand Down Expand Up @@ -152,4 +153,15 @@ export class NString {
static generateRandomString(length: number, pattern: string): string {
return generateRandomStringFn(length, pattern)
}

/**
* Generate Password
* @param length - given length
* @example
* const password1 = generatePassword(12);
* @returns { string } - random password
*/
static generatePassword(length: number): string {
return generatePasswordFn(length)
}
}

0 comments on commit b73df7a

Please sign in to comment.