Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makepanic improve url detection #140

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 20
node-version: 18

- name: Install dependencies
run: yarn install
Expand Down
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"ts",
"tsx",
"js"
]
],
"testEnvironment": "jsdom"
},
"repository": {
"type": "git",
Expand All @@ -48,20 +49,21 @@
},
"homepage": "https://github.com/mohsen1/json-formatter-js#readme",
"devDependencies": {
"@types/jest": "^24.0.19",
"css-loader": "^3.2.0",
"jest": "^28.1.0",
"less": "^3.10.3",
"less-loader": "^5.0.0",
"@types/jest": "^29.5.12",
"css-loader": "^7.1.2",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"less": "^4.2.0",
"less-loader": "^12.2.0",
"prettier": "^3.3.2",
"rollup": "^1.25.1",
"rollup-plugin-less": "^1.1.2",
"rollup-plugin-livereload": "^1.0.4",
"rollup-plugin-serve": "^1.0.1",
"rollup-plugin-terser": "^5.1.2",
"rollup-plugin-typescript2": "^0.24.3",
"ts-jest": "^28.0.2",
"typescript": "^3.6.4",
"webpack-dev-server": "^4.9.0"
"ts-jest": "^29.1.5",
"typescript": "^5.5.2",
"webpack-dev-server": "^5.0.4"
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const DATE_STRING_REGEX =
/(^\d{1,4}[\.|\\/|-]\d{1,2}[\.|\\/|-]\d{1,4})(\s*(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d\s*[ap]m)?$/;
const PARTIAL_DATE_REGEX = /\d{2}:\d{2}:\d{2} GMT-\d{4}/;
const JSON_DATE_REGEX = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/;
const URL_REGEX = /^https?:\/\//;

// When toggleing, don't animated removal or addition of more than a few items
const MAX_ANIMATED_TOGGLE_ITEMS = 10;
Expand Down Expand Up @@ -169,7 +170,7 @@ export default class JSONFormatter {
* is this a URL string?
*/
private get isUrl(): boolean {
return this.type === "string" && this.json.indexOf("http") === 0;
return this.type === "string" && URL_REGEX.test(this.json);
}

/*
Expand Down
10 changes: 10 additions & 0 deletions test/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ describe("url string", () => {
});
});

describe("url detection shouldn't generate false positive for words starting with http", () => {
const formatter = new JSONFormatter("httpstatus");

it('should not make a link and add class "url"', () => {
expect(formatter.render().querySelector("a.json-formatter-url")).toEqual(
null,
);
});
});

describe("object with empty property", () => {
const formatter = new JSONFormatter({ "": true });

Expand Down
Loading
Loading