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

Add web-utils package #24

Merged
merged 11 commits into from
Jan 17, 2024
Merged
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
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
"packages/*"
],
"scripts": {
"ci": "yarn prettier && yarn compile && yarn lint && yarn test && yarn build",
"ci": "yarn prettier && yarn lint && yarn dbuild && yarn test",
"clean": "rimraf node_modules -g 'packages/*/.eslintcache' 'packages/*/*.tsbuildinfo' 'packages/*/dist' 'packages/*/.rollup.cache' 'packages/*/types' 'packages/*/coverage'",
"compile": "yarn workspace @dolthub/react-hooks compile",
"build": "yarn workspace @dolthub/react-hooks build",
"lint": "yarn workspace @dolthub/react-hooks lint",
"prettier": "yarn workspace @dolthub/react-hooks prettier",
"test": "yarn workspace @dolthub/react-hooks test"
"dbuild": "yarn workspace @dolthub/web-utils dbuild && yarn workspace @dolthub/react-hooks dbuild",
"lint": "yarn workspace @dolthub/react-hooks lint && yarn workspace @dolthub/web-utils lint",
"prettier": "yarn workspace @dolthub/react-hooks prettier && yarn workspace @dolthub/web-utils prettier",
"test": "yarn workspace @dolthub/react-hooks test && yarn workspace @dolthub/web-utils test"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.18.1",
Expand Down
10 changes: 6 additions & 4 deletions packages/hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@dolthub/react-hooks",
"author": "DoltHub",
"description": "A collection of React hooks for common tasks",
"version": "0.1.6",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down Expand Up @@ -31,6 +32,11 @@
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"dependencies": {
"@dolthub/web-utils": "^0.1.2",
"js-cookie": "^3.0.5",
"react-hotkeys": "^2.0.0"
},
"devDependencies": {
"@babel/core": "^7.23.7",
"@babel/preset-env": "^7.23.8",
Expand Down Expand Up @@ -74,9 +80,5 @@
},
"bugs": {
"url": "https://github.com/dolthub/react-library/issues"
},
"dependencies": {
"js-cookie": "^3.0.5",
"react-hotkeys": "^2.0.0"
}
}
11 changes: 1 addition & 10 deletions packages/hooks/src/useSessionQueryHistory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { safeJSONParse } from "@dolthub/web-utils";
import { useState } from "react";
import useStateWithSessionStorage from "./useStateWithSessionStorage";

Expand Down Expand Up @@ -68,13 +69,3 @@ export default function useSessionQueryHistory(
queryIdx,
};
}

// safeJSONParse attempts to parse a JSON string, and if it fails returns the
// original string instead of throwing an error
function safeJSONParse(text: string): any {
try {
return JSON.parse(text);
} catch (_) {
return text;
}
}
5 changes: 4 additions & 1 deletion packages/hooks/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"composite": true,
"noImplicitAny": true,
"types": ["node", "jsdom", "@testing-library/jest-dom"]
}
},
"references": [
{ "path": "../utils" }
]
}
4 changes: 4 additions & 0 deletions packages/utils/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist
node_modules
.eslintrc.js
./*.js
3 changes: 3 additions & 0 deletions packages/utils/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: "../../.eslintrc.cjs",
};
2 changes: 2 additions & 0 deletions packages/utils/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/
tsconfig.*
15 changes: 15 additions & 0 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @dolthub/web-utils

A library of useful utilities for the web.

## Installation

```
% yarn add @dolthub/web-utils
```

or

```
% npm install @dolthub/web-utils
```
6 changes: 6 additions & 0 deletions packages/utils/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
18 changes: 18 additions & 0 deletions packages/utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const TEST_REGEX = "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|js?|tsx?|ts?)$";

module.exports = {
testRegex: TEST_REGEX,
transform: {
"^.+\\.tsx?$": "babel-jest",
},
testPathIgnorePatterns: [
"types",
"node_modules",
".rollup.cache",
"dist",
"helpers",
],
moduleFileExtensions: ["ts", "js"],
collectCoverage: false,
clearMocks: true,
};
64 changes: 64 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "@dolthub/web-utils",
"author": "DoltHub",
"description": "A collection of utilities for building web applications",
"version": "0.1.2",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"keywords": [
"web",
"utils",
"frontend"
],
"packageManager": "[email protected]",
"scripts": {
"compile": "tsc -b",
"build": "rollup -c --bundleConfigAsCjs",
"dbuild": "yarn compile && yarn build",
"lint": "eslint --cache --ext .ts,.js,.tsx,.jsx src",
"prettier": "prettier --check 'src/**/*.{js,ts}'",
"prettier-fix": "prettier --write 'src/**/*.{js,ts}'",
"npm:publish": "yarn dbuild && npm publish",
"test": "jest",
"yalc:publish": "yarn dbuild && yalc publish",
"yalc:push": "yarn dbuild && yalc push"
},
"devDependencies": {
"@babel/core": "^7.23.7",
"@babel/preset-env": "^7.23.8",
"@babel/preset-typescript": "^7.23.3",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.5",
"@types/eslint": "^8",
"@types/jest": "^29.5.11",
"@types/rollup-plugin-peer-deps-external": "^2",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.18.1",
"babel-jest": "^29.7.0",
"eslint": "^8.56.0",
"jest": "^29.7.0",
"prettier": "^3.1.0",
"rollup": "^4.9.4",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-terser": "^7.0.2",
"typescript": "^5.3.3",
"yalc": "^1.0.0-pre.53"
},
"repository": {
"type": "git",
"url": "git+https://github.com/dolthub/react-library.git"
},
"bugs": {
"url": "https://github.com/dolthub/react-library/issues"
},
"dependencies": {
"node-sql-parser": "^4.17.0",
"timeago.js": "^4.0.2"
}
}
40 changes: 40 additions & 0 deletions packages/utils/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import { terser } from "rollup-plugin-terser";
import external from "rollup-plugin-peer-deps-external";
import { dts } from "rollup-plugin-dts";

const packageJson = require("./package.json");

export default [
{
input: "src/index.ts",
output: [
{
file: packageJson.main,
format: "cjs",
sourcemap: true,
name: "utils-ts-lib",
},
{
file: packageJson.module,
format: "esm",
sourcemap: true,
},
],
external: ["querystring", "url"],
plugins: [
external(),
nodeResolve(),
commonjs(),
typescript({ tsconfig: "./tsconfig.json", outputToFilesystem: true }),
terser(),
],
},
{
input: "./types/index.d.ts",
output: [{ file: "dist/index.d.ts", format: "esm" }],
plugins: [dts()],
},
];
2 changes: 2 additions & 0 deletions packages/utils/src/Maybe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type Maybe<T> = T | undefined | null;
export default Maybe;
39 changes: 39 additions & 0 deletions packages/utils/src/__tests__/dateConversions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// const dateTests: Array<{
// date: Date;
// expectedDate: string;
// }> = [
// {
// date: getDateAtTime(1, 2, 4, 30),
// expectedDate: "2022-01-02T04:30",
// },
// {
// date: getDateAtTime(9, 30, 22, 1),
// expectedDate: "2022-09-30T22:01",
// },
// {
// date: getDateAtTime(12, 31, 23, 58),
// expectedDate: "2022-12-31T23:58",
// },
// ];

describe("test date conversions", () => {
it("should pass", () => {});
// dateTests.forEach(test => {
// it(`gets date and time string for ${test.date.toLocaleDateString()}`, () => {
// expect(getDateAndTimeString(test.date)).toEqual(test.expectedDate);
// });
// });
});

// function getDateAtTime(
// month: number,
// day: number,
// hours: number,
// minutes: number,
// ): Date {
// const date = new Date();
// // Months are 0 indexed
// date.setFullYear(2022, month - 1, day);
// date.setHours(hours, minutes);
// return date;
// }
32 changes: 32 additions & 0 deletions packages/utils/src/__tests__/dedupe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import dedupe from "../dedupe";

const tests: Array<{ desc: string; arr: any[]; expect: any[] }> = [

Check warning on line 3 in packages/utils/src/__tests__/dedupe.test.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected any. Specify a different type

Check warning on line 3 in packages/utils/src/__tests__/dedupe.test.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected any. Specify a different type
{
desc: "no duplicates",
arr: ["a", "b", "c", undefined],
expect: ["a", "b", "c", undefined],
},
{
desc: "array of strings",
arr: ["a", "b", "c", "b"],
expect: ["a", "b", "c"],
},
{
desc: "array of numbers",
arr: [5, 9, 200, 200, 90, 0, 200],
expect: [5, 9, 200, 90, 0],
},
{
desc: "array of mixed",
arr: [true, 5, 9, "a", false, 200, 90, 0, "a", 200, true],
expect: [true, 5, 9, "a", false, 200, 90, 0],
},
];

describe("test dedupe", () => {
tests.forEach(test => {
it(`dedupes ${test.desc}`, () => {
expect(dedupe(test.arr)).toEqual(test.expect);
});
});
});
18 changes: 18 additions & 0 deletions packages/utils/src/__tests__/enumKeys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import enumKeys from "../enumKeys";

describe("test enumKeys", () => {
it("iterates through all keys in an enum", () => {
enum TestEnum {
Key1 = "value1",
Key2 = "value2",
Key3 = "value3",
}

const keys = enumKeys(TestEnum);
const t = keys.map(k => TestEnum[k]);

expect(t).toContain(TestEnum.Key1);
expect(t).toContain(TestEnum.Key2);
expect(t).toContain(TestEnum.Key3);
});
});
11 changes: 11 additions & 0 deletions packages/utils/src/__tests__/excerpt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import excerpt from "../excerpt";

test("does not modify a string shorter than the specified length", () => {
expect(excerpt("shorter string", 100)).toBe("shorter string");
});

test("truncates an excerpt longer than the specified length", () => {
expect(excerpt("there are 38 characters in this string", 30)).toBe(
"there are 38 characters in th…",
);
});
Loading
Loading