Skip to content

Commit 2295d74

Browse files
committed
first commit
1 parent ded5281 commit 2295d74

17 files changed

+8039
-0
lines changed

.eslintrc.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
"extends": [
3+
"react-app",
4+
"prettier/@typescript-eslint",
5+
"plugin:prettier/recommended"
6+
],
7+
"settings": {
8+
"react": {
9+
"version": "999.999.999"
10+
}
11+
}
12+
}

.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff
2+
.idea/*
3+
.nyc_output
4+
build
5+
coverage
6+
package-lock.json
7+
.timetracker.txt
8+
scratch/
9+
10+
*.log
11+
.DS_Store
12+
node_modules
13+
dist

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 karacas
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

package.json

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"name": "@karacas/kaysh",
3+
"version": "0.1.38",
4+
"author": "karacas",
5+
"module": "dist/mylib.esm.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/karacas/kaysh"
9+
},
10+
"homepage": "https://github.com/karacas/kaysh",
11+
"license": "MIT",
12+
"main": "dist/index.js",
13+
"typings": "dist/index.d.ts",
14+
"files": [
15+
"dist",
16+
"src"
17+
],
18+
"engines": {
19+
"node": ">=14"
20+
},
21+
"scripts": {
22+
"start": "tsdx watch",
23+
"build": "tsdx build",
24+
"test": "tsdx test",
25+
"test:watch": "tsdx test --watch",
26+
"test:coverage": "tsdx test --coverage",
27+
"test_clearCache": "npx jest --clearCache",
28+
"lint": "tsdx lint",
29+
"prepare": "tsdx build",
30+
"size": "size-limit",
31+
"analyze": "size-limit --why"
32+
},
33+
"peerDependencies": {},
34+
"husky": {
35+
"hooks": {
36+
"pre-commit": "tsdx lint"
37+
}
38+
},
39+
"prettier__": {
40+
"printWidth": 140,
41+
"semi": true,
42+
"singleQuote": true,
43+
"trailingComma": "es5"
44+
},
45+
"size-limit": [
46+
{
47+
"path": "dist/mylib.cjs.production.min.js",
48+
"limit": "20 KB"
49+
},
50+
{
51+
"path": "dist/mylib.esm.js",
52+
"limit": "20 KB"
53+
}
54+
],
55+
"dependencies": {
56+
"rxjs": "^6.6.0"
57+
},
58+
"devDependencies": {
59+
"@size-limit/preset-small-lib": "^7.0.5",
60+
"husky": "^7.0.4",
61+
"size-limit": "^7.0.5",
62+
"tsdx": "^0.14.1",
63+
"tslib": "^2.3.1",
64+
"typescript": "^4.5.5"
65+
}
66+
}

prettier.config.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const rxjs_cache = {
2+
printWidth: 140,
3+
singleQuote: true,
4+
tabWidth: 2,
5+
trailingComma: 'es5',
6+
semi: true,
7+
proseWrap: 'always',
8+
arrowParens: 'avoid',
9+
overrides: [
10+
{
11+
files: '*.html',
12+
options: {
13+
printWidth: 120,
14+
parser: 'html',
15+
proseWrap: 'preserve',
16+
htmlWhitespaceSensitivity: 'ignore',
17+
bracketSpacing: true,
18+
arrowParens: 'avoid',
19+
},
20+
},
21+
{
22+
files: '*.scss',
23+
options: {
24+
parser: 'scss',
25+
},
26+
},
27+
],
28+
};
29+
30+
module.exports = rxjs_cache;

src/.types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare var __DEV__: boolean;

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { __rxjsCacheStore } from './lib/rxjs-cache-store';
2+
import { __simpleCacheStore } from './lib/simple-cache-store';
3+
export { __simpleCacheStore as kaysh, __rxjsCacheStore as rxjsKaysh };

src/lib/_aux.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export function __strToHash(str = ''): string {
2+
let hash = 0,
3+
i,
4+
chr;
5+
6+
for (i = 0; i < str.length; i++) {
7+
chr = str.charCodeAt(i);
8+
hash = (hash << 5) - hash + chr;
9+
hash |= 0;
10+
}
11+
12+
return hash.toString(36);
13+
}
14+
15+
function getShortValue(val: any) {
16+
if (Number.isInteger(val) && String(val).length) return 'num_' + val;
17+
18+
if (typeof val === 'string') {
19+
if (val.length === 0) return 'emptyString__';
20+
return 'str_' + val;
21+
}
22+
23+
if (Array.isArray(val)) {
24+
if (val.length === 0) return 'emptyArray__';
25+
26+
if (val.length === 1) {
27+
if (Number.isInteger(val[0]) && String(val[0]).length) return '_num_' + val[0];
28+
29+
if (typeof val[0] === 'string') {
30+
if (val[0].length === 0) return '_emptyString__';
31+
return '_str_' + val[0];
32+
}
33+
}
34+
}
35+
36+
return null;
37+
}
38+
39+
export function __objToHash(val: any, fast = true) {
40+
if (val === undefined) return '__undefined__';
41+
if (val === null) return '__null__';
42+
43+
if (fast) {
44+
let shortValue = getShortValue(val);
45+
if (shortValue != null && shortValue.length > 0 && shortValue.length < 32) return shortValue;
46+
}
47+
48+
return __strToHash(JSON.stringify(val));
49+
}

src/lib/rxjs-cache-store.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { of } from 'rxjs';
2+
import { Observable } from 'rxjs/internal/Observable';
3+
import { isObservable } from 'rxjs/internal/util/isObservable';
4+
import { shareReplay, tap } from 'rxjs/operators';
5+
import { DefaultConfigCacheModel, __simpleCacheStore } from './simple-cache-store';
6+
7+
const getRxjsObservableCacheValue = (key: string, payloadToHash?: any): Observable<any> => {
8+
const cache = __simpleCacheStore.getCacheValue(key, payloadToHash);
9+
10+
if (cache == null) return cache;
11+
12+
if (isObservable(cache)) return cache;
13+
14+
return of(cache);
15+
};
16+
17+
const setRxjsObservableCacheValue = (
18+
stream: Observable<any>,
19+
key: string,
20+
payloadToHash?: any,
21+
config?: DefaultConfigCacheModel
22+
): Observable<any> => {
23+
if (stream == null || key == null) return of(null);
24+
25+
const setCache = ($data: any) => {
26+
if ($data != null) __simpleCacheStore.setCacheValue(key, $data, payloadToHash, config);
27+
};
28+
29+
setCache(stream.pipe(shareReplay()));
30+
31+
return stream.pipe(
32+
tap((_data: any) => setCache(_data)),
33+
shareReplay()
34+
);
35+
};
36+
37+
const __rxCacheStore = {
38+
getRxjsObservableCacheValue,
39+
setRxjsObservableCacheValue,
40+
resetCache: __simpleCacheStore.resetCache,
41+
resetAllCaches: __simpleCacheStore.resetAllCaches,
42+
};
43+
44+
export { __rxCacheStore as __rxjsCacheStore };

0 commit comments

Comments
 (0)