Skip to content

Commit

Permalink
0.4.0 Move to export src instead of dist files
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsukugi committed Sep 6, 2024
1 parent 18fb2ed commit 3b4a891
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 26 deletions.
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#Development folders
src
examples
tests
typedoc
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
{
"name": "@atsu/taihou",
"version": "0.3.3",
"version": "0.4.0",
"description": "Small state manager written in Typescript",
"main": "./dist/umd.bundle.js",
"exports": {
"node": {
"import": "./dist/commonjs2.bundle.cjs",
"require": "./dist/commonjs.bundle.cjs"
},
"default": "./dist/umd.bundle.js"
},
"types": "./dist/index.d.ts",
"main": "./src/index.ts",
"scripts": {
"compile": "tsc",
"test": "jest --config jest.config.js",
Expand Down
28 changes: 16 additions & 12 deletions src/store/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,31 @@ export const deepProxy = <RootObject extends object>(
path: string,
onChange: (props: OnChangeWrapperProps<RootObject>) => void,
) => {
const addProperty = (property) =>
const addProperty = (property: string) =>
path !== "" ? `${path}.${property}` : property;

return new Proxy(target, {
get(target, property) {
const item = target[property as string];
const key = property as keyof RootObject;
const item = target[key];
if (!item || typeof item !== "object") return item;
if (proxyCache.has(item)) return proxyCache.get(item);
const proxy = createDeepOnChangeProxy(
item as RootObject,
addProperty(property),
addProperty(String(key)),
onChange,
);
proxyCache.set(item, proxy);
return proxy;
},
set(target, property: string, newValue) {
set(target, property, newValue) {
const key = property as keyof RootObject;
onChange({
initialObject,
path: addProperty(property),
path: addProperty(String(key)),
newValue,
});
(target as unknown)[property] = newValue;
target[key] = newValue;
return true;
},
});
Expand All @@ -48,14 +50,16 @@ export const deepProxy = <RootObject extends object>(
return createDeepOnChangeProxy(initialObject, "", onChangeWrapper);
};

export const updateNestedValue = <T>(
initialObject: T,
export const updateNestedValue = <
RootObject extends Record<keyof RootObject, unknown>,
>(
initialObject: RootObject,
path: string,
newValue: unknown,
): T => {
): RootObject => {
const keys = path.split(".");
const updatedObject: T = { ...initialObject };
let currentObject: T = updatedObject;
const updatedObject: RootObject = { ...initialObject };
let currentObject: Record<string, unknown> = updatedObject;

for (let i = 0; i < keys.length - 1; i++) {
if (
Expand All @@ -64,7 +68,7 @@ export const updateNestedValue = <T>(
) {
currentObject[keys[i]] = {};
}
currentObject = currentObject[keys[i]];
currentObject = currentObject[keys[i]] as Record<string, unknown>;
}

currentObject[keys[keys.length - 1]] = newValue;
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("Taihou Store", () => {
test("Is the state updated", () => {
const history: number[] = [];

const updateHistory = ({ count }) => {
const updateHistory = ({ count }: { count: number }) => {
history.push(count);
expect(history).toContain(count);
};
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"noEmit": true,
"strict": true,
"types": ["node"]
},
"include": ["src/**/*.ts"],
Expand Down

0 comments on commit 3b4a891

Please sign in to comment.