Skip to content

Commit

Permalink
chore(refactor): اصلاح برخی موارد
Browse files Browse the repository at this point in the history
feat(refactor): اضافه شدن تست مرورگر

Signed-off-by: MD Amanalikhani <md.akhi.ir@gmail.com>
  • Loading branch information
md-akhi committed Oct 27, 2023
1 parent 4ece537 commit a179f8a
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 86 deletions.
43 changes: 26 additions & 17 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ function replaceDocBlockInfo() {
)
.pipe(
replace(
/version: string = "([\w\d:\/\.\-]+)"/g,
`version: string = "${pkg.version}"`
/VERSION: string = "([\w\d:\/\.\-]+)"/g,
`VERSION: string = "${pkg.version}"`
)
)
.pipe(gulp.dest("src/"));
}

function cleanCompile(cb) {
return del(["dist"], cb);
return del(["dist", "src/browser"], cb);
}

function compileCjs(cb) {
Expand Down Expand Up @@ -125,17 +125,10 @@ function concatBrowserTS() {
return gulp
.src(["./src/languages/**/*.ts", "./src/parser/**/*.ts", "src/*.ts"])
.pipe(concat("shdate.ts"))
.pipe(replace(/export default (function|class)/g, "$1"))
.pipe(replace(/import [a-zA-Z_]* from [0-9a-zA-Z_/\.\"]*;/g, " "))
.pipe(replace(/extends (Language)/g, "extends SHDate$1"))
.pipe(replace(/class ([a-z]{2,3}_[A-Z]{2})/g, "class SHDateLanguage_$1"))
.pipe(replace(/ ([a-z]{2,3}_[A-Z]{2})\./g, " SHDateLanguage_$1."))
.pipe(replace(/ Language_([a-z]{2,3}_[A-Z]{2})/g, " SHDateLanguage_$1"))
.pipe(replace(/\((Languages)\)/g, "(SHDate$1)"))
.pipe(replace(/class (Language|Word)/g, "class SHDate$1"))
.pipe(replace(/(Language\.|Word\.)/g, "SHDate$1"))
.pipe(replace(/enum (Language)/g, "enum SHDate$1"))
.pipe(replace(/ Languages.([a-z]{2,3}_[A-Z]{2})/g, " SHDateLanguages.$1"))
.pipe(replace(/export (default)?/g, ""))
.pipe(replace(/import [a-zA-Z_,{ }]* from [0-9a-zA-Z_\/\.\"]*;/g, ""))
.pipe(replace(/(Languages?|Word)(\.)?/g, "SHDate$1$2"))
.pipe(replace(/ ([a-z]{2,3}_[A-Z]{2})(\.)?/g, " SHDateLanguage_$1$2"))
.pipe(replace(/SH(Parser|Lexer|Token)/g, "SHDate$1"))
.pipe(gulp.dest("src/browser"));
}
Expand Down Expand Up @@ -187,7 +180,7 @@ exports.default = gulp.task(
)
);

function cleanDev(cb) {
function cleanTest(cb) {
return del(["dist/src", "dist/tests"], cb);
}

Expand All @@ -203,6 +196,20 @@ function compileTest(cb) {
}
gulp.task(compileTest);

function copyTestBrowser(cb) {
return gulp.src(["tests/browser/*"]).pipe(gulp.dest("dist/tests/browser"));
}
function installTestsBrowser(cb) {
return exec(
"(cd dist/tests/browser && npm i && npm test)",
function (err, stdout, stderr) {
// console.log(stdout);
// console.log(stderr);
cb(err);
}
);
}

function copyTestCJS(cb) {
return gulp.src(["tests/cjs/*"]).pipe(gulp.dest("dist/tests/cjs"));
}
Expand All @@ -216,6 +223,7 @@ function installTestsCJS(cb) {
}
);
}

function copyTestMJS(cb) {
return gulp.src(["tests/mjs/*"]).pipe(gulp.dest("dist/tests/mjs"));
}
Expand Down Expand Up @@ -246,17 +254,18 @@ function installTestsPKGS(cb) {
gulp.task(
"test",
gulp.series(
cleanDev,
cleanTest,
compileTest,
gulp.parallel(
gulp.series(copyTestBrowser, installTestsBrowser),
gulp.series(copyTestCJS, installTestsCJS),
gulp.series(copyTestMJS, installTestsMJS),
gulp.series(copyTestPKG, installTestsPKGS)
)
)
);

gulp.task("testRelease", gulp.series(cleanDev, compileTest));
gulp.task("testRelease", gulp.series(cleanTest, compileTest));

// buildUntilTests.displayName = 'clean:all';
// buildUntilTests.description = 'Build the project';
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"watch": "tsc -w",
"build": "gulp build",
"test": "gulp test && mocha",
"test:release": "gulp testRelease && mocha",
"dev:test": "nodemon --exec npm test",
"test:release": "gulp testRelease && mocha",
"test:build": "gulp compileTest && node dist/tests/build-test.js > tests/build-test.log",
"prepare": "husky install"
},
Expand Down Expand Up @@ -127,13 +127,11 @@
"gulp-header": "^2.0.9",
"gulp-rename": "^2.0.0",
"gulp-replace": "^1.1.4",
"gulp-typescript": "^6.0.0-alpha.1",
"gulp-uglify": "^3.0.2",
"husky": "^8.0.3",
"lint-staged": "^14.0.1",
"mocha": "^10.2.0",
"nodemon": "^3.0.1",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
"tslib": "^2.6.2",
"typescript": "^5.2.2"
Expand Down
2 changes: 1 addition & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class SHDate {
/**
* version of SHDate
*/
static VERSION: string = "2.1.05";
static VERSION: string = "2.1.24";

/**
* @type {number[]} days in month without leap year
Expand Down
4 changes: 2 additions & 2 deletions src/languages/i18n/fa_IR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class fa_IR {
"ارديبهشت",
"خرداد",
"تير",
"امرداد",
"مرداد",
"شهريور",
"مهر",
"آبان",
Expand All @@ -52,7 +52,7 @@ export default class fa_IR {
"ارد",
"خرد",
"تير",
"امر",
"مرد",
"شهر",
"مهر",
"آبا",
Expand Down
2 changes: 1 addition & 1 deletion src/languages/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Language_ckb_IR from "./l10n/ckb_IR.js";
/**
* list of supported languages
*/
enum Languages {
export enum Languages {
//i18n
fa_IR = "fa_IR",
en_US = "en_US",
Expand Down
27 changes: 15 additions & 12 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @link http://git.akhi.ir/js/SHDate | https://github.com/md-akhi/SHDateTime-js#readme
*/

import Language from "./languages/language.js";
import Language, { Languages } from "./languages/language.js";

/**
* Convert a number to Persian digit
Expand All @@ -14,27 +14,30 @@ import Language from "./languages/language.js";
* @returns {string} The converted string.
*/
export function toNumber(
source: string,
destination: string | string[] = "FA",
comma: string = ","
source: string | number,
destination: string = "FA"
// comma: string = ","
): string {
const EN: string[] = Language.getClass("en_US").DIGIT;
const FA: string[] = Language.getClass("fa_IR").DIGIT;
switch (destination) {
const EN: string[] = Language.getClass(Languages.en_US).DIGIT;
const FA: string[] = Language.getClass(Languages.fa_IR).DIGIT;
let digits: string[];
switch (destination.toUpperCase()) {
case "FA":
destination = FA;
case "FA_IR":
digits = FA;
break;
case "EN":
destination = EN;
case "EN_US":
digits = EN;
break;
default:
destination = FA;
digits = FA;
break;
}
EN.forEach((value, index) => {
source = source.toString().replace(value, destination[index]);
source = `${source}`.replace(value, digits[index]);
});
// for (const [index, value] of EN.entries()) {
// source = source.split(value).join(destination[index]);
return source;
return `${source}`;
}
3 changes: 2 additions & 1 deletion tests/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<body>
<script
type="text/javascript"
src="../../dist/browser/shdate.min.js"
src="./node_modules/shdate/dist/browser/shdate.min.js"
></script>

<script>
var shdate = new SHDate();
document.body.innerHTML = shdate.toDateString();
Expand Down
15 changes: 15 additions & 0 deletions tests/browser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "tests",
"version": "1.0.0",
"description": "",
"main": "test.cjs",
"scripts": {
"test": "index.html"
},
"author": "",
"license": "ISC",
"dependencies": {
"shdate": "file:../../.."
},
"type": "commonjs"
}
46 changes: 5 additions & 41 deletions tests/pkgs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,28 @@
import { assert, expect } from "chai";
import { it, describe } from "mocha";

import { exec } from "node:child_process";
import SHDate from "../src/base.js";
import words from "../src/word.js";
import { toNumber } from "../src/tools.js";
import en_US from "../src/languages/i18n/en_US.js";
import fa_IR from "../src/languages/i18n/fa_IR.js";
import ckb_IR from "../src/languages/l10n/ckb_IR.js";
import * as fs from "fs";

import * as fs from "node:fs";
const dataYear = 1402;

async function execPromise(command = "") {
return await new Promise(function (resolve, reject) {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
resolve(stdout.trim());
});
});
}

// cjs
const wordFileCJS = fs
.readFileSync("./dist/tests/cjs/test.log", "utf-8")
.trim();
// execPromise("(node dist/tests/cjs/test.cjs)")
// .then((rs) => {
// data.cjs = rs;
// })
// .catch(function (err) {
// console.error(err.message);
// });

// mjs
const wordFileMJS = fs
.readFileSync("./dist/tests/mjs/test.log", "utf-8")
.trim();
// execPromise("(node dist/tests/mjs/test.mjs)")
// .then((rs) => {
// data.mjs = rs;
// })
// .catch(function (err) {
// console.error(err.message);
// });

// pkgs
const wordFilePKGS = fs
.readFileSync("./dist/tests/pkgs/test.log", "utf-8")
.trim();
// execPromise("(node dist/tests/pkgs/test.mjs)")
// .then((rs) => {
// data.pkg = rs;
// })
// .catch(function (err) {
// console.error(err.message);
// });

describe("test CommonJS (CJS) && ES Modules (ESM) && exports pkgs", () => {
it("run CJS", () => {
Expand All @@ -69,10 +34,9 @@ describe("test CommonJS (CJS) && ES Modules (ESM) && exports pkgs", () => {
it("run exports pkgs", () => {
assert.equal(
wordFilePKGS,
`${SHDate.VERSION} ${words.word_language}`
// toNumber(123),
// en_US.LANGUAGE,
// ckb_IR.LANGUAGE
`${SHDate.VERSION} ${words.word_language} ${toNumber("١٢٣", "en")} ${
fa_IR.LANGUAGE
} ${ckb_IR.LANGUAGE}`
);
});
});
8 changes: 4 additions & 4 deletions tests/pkgs/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import ckb_IR from "shdate/lang/l10n/ckb_IR";

console.log(
SHDate.VERSION,
words.word_language
// // toNumber,
// fa_IR.LANGUAGE,
// ckb_IR.LANGUAGE
words.word_language,
toNumber("١٢٣", "en"),
fa_IR.LANGUAGE,
ckb_IR.LANGUAGE
);
8 changes: 4 additions & 4 deletions tsconfig.browser.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"moduleResolution": "node10",
"module": "umd",
"outDir": "dist/browser",
"target": "es2022",
"lib": ["dom", "es2022"],
"target": "es6",
"lib": ["dom", "es6"],
"esModuleInterop": false,
"noImplicitAny": true,
"sourceMap": true,
"declaration": true,
"sourceMap": false,
"declaration": false,
"removeComments": true
},
"compileOnSave": false,
Expand Down

0 comments on commit a179f8a

Please sign in to comment.