Skip to content

Commit

Permalink
fix(nammatham): fix bun error on windows when using default import
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed May 16, 2024
1 parent 0e0ee34 commit a792187
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 24 deletions.
39 changes: 39 additions & 0 deletions docs/bun.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Bun

## Issues

Summary:
- `lodash_default is not a function` error when using `lodash.camelcase` package
- Using default import of `lodash` package may cause this issue

Deployment at:
- https://github.com/thaitype/nammatham/actions/runs/9105382778
- Executable File from `bun-win-x64` target
- Using bun to compile into single executable file

Platform:

- Azure Functions Windows
- Bun v1.1.8 (Windows x64)

Package:

- lodash.camelcase v4.3.0
- nammatham on [commit 3f3b7b7 (2024-05-15)](https://github.com/thaitype/nammatham/commit/96ad8958c843553b0156d968b72e1d285c85041f)
- Build with tsup v8.0.1

```
Bun v1.1.8 (Windows x64)
n/a
at B:/~BUN/root/main.exe:3061:12
at http (B:/~BUN/root/main.exe:2983:5)
at http (B:/~BUN/root/main.exe:3031:13)
TypeError: lodash_default is not a function. (In 'lodash_default(options.name ?? options.route)', 'lodash_default' is undefined)
^
3031 | name: lodash_default(options.name ?? options.route),
3030 | this.functions.push({
3029 | }
3028 | throw new Error("Route or Name is required");
3027 | if (options.route === undefined && options.name === undefined) {
3026 | http(options) {
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"tsup": "^8.0.1",
"tsx": "^4.7.0",
"typescript": "^5.3.3",
"vitest": "^1.1.3"
"vitest": "^1.2.1"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
Expand Down
4 changes: 1 addition & 3 deletions packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "vitest",
"prepublishOnly": "npm run build",
"build": "run-s build:*",
"build:package": "tsup src/main.ts --dts --format esm --external bun,@types/bun",
Expand All @@ -40,7 +40,6 @@
"esbuild": "^0.20.2",
"execa": "^8.0.1",
"invariant": "^2.2.4",
"lodash.camelcase": "^4.3.0",
"lodash.merge": "^4.6.2",
"pino": "^8.17.1",
"pino-dev": "^4.0.3",
Expand All @@ -60,7 +59,6 @@
},
"devDependencies": {
"@types/debug": "^4.1.12",
"@types/lodash.camelcase": "^4.3.9",
"@types/lodash.merge": "^4.6.9",
"hono": "^4.3.6"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/main/src/nammatham.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import camelCase from 'lodash.camelcase';

import type { NammathamFunction, HttpTriggerOptions, NammathamTrigger } from './types';

import { pascalCase } from './utils';

export class Nammatham implements NammathamTrigger {
protected functions: NammathamFunction[] = [];

Expand All @@ -24,7 +24,7 @@ export class Nammatham implements NammathamTrigger {
throw new Error('Route or Name is required');
}
this.functions.push({
name: camelCase(options.name ?? options.route),
name: pascalCase(options.name ?? options.route),
metadata: {
bindings: [
{
Expand Down
31 changes: 31 additions & 0 deletions packages/main/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, test } from 'vitest';
import { pascalCase } from './utils';

describe('pascalCase', () => {
test('converts undefined to an empty string', () => {
expect(pascalCase(undefined)).toBe('');
});
test('converts a simple sentence to PascalCase', () => {
expect(pascalCase('convert any string to pascal case')).toBe('ConvertAnyStringToPascalCase');
});

test('handles special characters correctly', () => {
expect(pascalCase('convert_any string-to!pascal@case')).toBe('ConvertAnyStringToPascalCase');
});

test('handles a single word correctly', () => {
expect(pascalCase('hello')).toBe('Hello');
});

test('handles an empty string correctly', () => {
expect(pascalCase('')).toBe('');
});

test('handles multiple spaces correctly', () => {
expect(pascalCase(' multiple spaces in string ')).toBe('MultipleSpacesInString');
});

test('handles numbers correctly', () => {
expect(pascalCase('convert 123 number')).toBe('Convert123Number');
});
});
9 changes: 9 additions & 0 deletions packages/main/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function pascalCase(str: string | undefined): string {
if (str === undefined) {
return '';
}
return str
.split(/[^a-zA-Z0-9]+/)
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join('');
}
18 changes: 1 addition & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a792187

Please sign in to comment.