Skip to content

Commit

Permalink
feat: Upgrade to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Michaelsen committed Nov 29, 2023
1 parent c2e8fb4 commit 763b5ea
Show file tree
Hide file tree
Showing 7 changed files with 5,821 additions and 14 deletions.
16 changes: 16 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html
module.exports = {
clearMocks: true,
resetMocks: true,
restoreMocks: true,
testEnvironment: "node",
transform: {
".(js|ts)": "ts-jest"
},
transformIgnorePatterns: [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx|json)$",
"package.json"
],
};

File renamed without changes.
17 changes: 9 additions & 8 deletions lib/generator.js → lib/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = (function () {
var jsomeRef
, browserColors = []
, browserColors: string[] = []
, browserStyle = require('./browser-style')

function getType (value) {
Expand Down Expand Up @@ -54,6 +54,7 @@ module.exports = (function () {
for (var key in obj) {
if (isArray(obj[key]) || isObject(obj[key])) return true;
}
return false;
}

function isArray (arr) {
Expand All @@ -64,15 +65,15 @@ module.exports = (function () {
return toString.call(obj).match(/^\[object Object\]$/);
}

function colorify (value, level) {
function colorify (value, level = 0) {
var color = jsomeRef.colors[getType(value)];
return generateLevel(level)
+ (getType(value) === 'str' ? colorifySpec('"', 'quot') : '')
+ useColorProvider('' + value, color)
+ (getType(value) === 'str' ? colorifySpec('"', 'quot') : '');
}

function colorifySpec (char, type, level) {
function colorifySpec (char, type, level = 0): string {
var quote = (
jsomeRef.params.lintable && type === 'attr'
? colorifySpec('"', 'quot', 0)
Expand Down Expand Up @@ -109,7 +110,7 @@ module.exports = (function () {

return {
gen : function (json, level, isChild) {
var colored = [];
var colored: string[] = [];
level = level || 0;

if (isObject(json)) {
Expand All @@ -133,19 +134,19 @@ module.exports = (function () {

if (hasChild(json)) {

var result = json.map(function(item) {
var arr = json.map(function(item) {
return this.gen(item, level+1);
}.bind(this));

colored.push(colorifySpec('[', 'brack', isChild ? 0 : level));;
colored.push(result.join(colorifySpec(', ', 'punc') + '\n' ));
colored.push(arr.join(colorifySpec(', ', 'punc') + '\n' ));
colored.push(colorifySpec(']', 'brack', level));

} else {

var coloredArray = colorifySpec('[', 'brack', isChild ? 0 : level);
for (var key in json) {
coloredArray += colorify(json[key]) + (json.length-1>key ? colorifySpec(', ', 'punc') : '');
for (var i = 0; i < json.length; i++) {
coloredArray += colorify(json[i]) + (json.length - 1 > i ? colorifySpec(', ', 'punc') : '');
}
colored.push(coloredArray + colorifySpec(']', 'brack'));

Expand Down
Loading

0 comments on commit 763b5ea

Please sign in to comment.