Skip to content

Commit

Permalink
build: 📦 รองรับ typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
daddybannk committed May 26, 2024
1 parent 8354a34 commit ed16da2
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 54 deletions.
3 changes: 3 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface String {
toCapitalCase(ignoreCase?: boolean): string;
}
File renamed without changes.
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<html>
<head>
<title>capital-case-r2</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>capital-case-r2</h1>
Expand All @@ -17,7 +19,7 @@ <h1>capital-case-r2</h1>
</p>
<p id="result" style="white-space: pre-line;"></p>

<script src="dist/capitalcase.js"></script>
<script src="dist/index.js"></script>
<script type="text/javascript">
function convertToCapital() {
const ignore = document.getElementById("ignoretrue").checked;
Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "capital-case-r2",
"version": "1.0.1",
"version": "2.0.0",
"description": "Additional JS `String.prototype` function.",
"main": "dist/capitalcase.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"clean": "rm -rf node_modules",
"test": "mocha",
"build": "webpack --config webpack.config.js --mode=production"
"build": "webpack"
},
"author": "Sahachai Y.",
"license": "MIT",
Expand All @@ -20,8 +21,12 @@
"capital-case"
],
"devDependencies": {
"mocha": "^10.2.0",
"chai": "^4.3.7",
"@types/node": "^20.12.12",
"@types/webpack": "^5.28.5",
"chai": "^4.4.1",
"mocha": "^10.4.0",
"ts-loader": "^9.5.1",
"typescript": "^5.4.5",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1"
}
Expand Down
41 changes: 0 additions & 41 deletions src/index.js

This file was deleted.

45 changes: 45 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
interface String {
toCapitalCase(ignoreCase?: boolean): string;
}

String.prototype.toCapitalCase = function (ignoreCase = true) {
function isCharacter(text: String) {
return text.search(/[^a-zA-Z]+/) === -1;
}
function foo(d: String) {
let state = 0;
const myarray = d.split("");
for (let i = 0; i < myarray.length; i++) {
const ch = myarray[i]
if (state == 0 && isCharacter(myarray[i])) {
myarray[i] = ch.toUpperCase()
state = 1;
}
else if (state == 0 && !isCharacter(myarray[i])) {
state = 3;
}
else if (state == 1 && isCharacter(myarray[i])) {
state = 2;
}
else if (state == 1 && !isCharacter(myarray[i])) {
state = 3;
}
else if (state == 2 && isCharacter(myarray[i])) {
state = 2;
}
else if (state == 2 && !isCharacter(myarray[i])) {
state = 3;
}
else if (state == 3 && isCharacter(myarray[i])) {
myarray[i] = ch.toUpperCase()
state = 1;
}
else if (state == 3 && !isCharacter(myarray[i])) {
state = 3;
}
}
return myarray.join("");
}
const value = ignoreCase ? this : this.toLowerCase()
return foo(value);
}
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var chai = require("chai");
var expect = chai.expect;

require("../src/index");
require("../dist/index");

describe("capital-case-r2", function() {
describe("Function `String.prototype.toCapitalCase(ignoreCase)`", function() {
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es6",
"module": "ES6",
"declaration": true,
"outDir": "./dist",
"strict": true
},
"include": ["src"],
"exclude": ["node_modules"]
}
35 changes: 29 additions & 6 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
var path = require('path');
const path = require('path');

module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'capitalcase.js'
mode: 'production', // or 'development'
entry: './src/index.ts',
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.ts', '.js']
},
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist'),
library: {
type: 'module'
},
}
environment: {
module: true
}
},
experiments: {
outputModule: true
}
};

0 comments on commit ed16da2

Please sign in to comment.