Skip to content

Commit

Permalink
more funcs + move to es2016 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodiray authored and aleclarson committed Jun 24, 2024
1 parent 8213adb commit a930270
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 69 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "1.0.4",
"version": "1.0.7",
"description": "A collection of useful and helpful functions",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
39 changes: 37 additions & 2 deletions src/curry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Func = (...args: any[]) => any | void
export type Func<TArgs = any, KReturn = any | void> = (...args: TArgs[]) => KReturn

export const chain = (...funcs: Func[]) => (...args: any[]) => {
return funcs.slice(1).reduce((acc, fn) => fn(acc), funcs[0](...args))
Expand Down Expand Up @@ -29,8 +29,43 @@ export const tryit = <ResultType, ErrorType = Error>(func: Func) => async (
}
}

export const proxied = <T, K> (cb: (arg: T) => K): Record<string, (arg: T) => K> => {
export const proxied = <T, K>(cb: (arg: T) => K): Record<string, (arg: T) => K> => {
return new Proxy({}, {
get: (target, arg: any) => cb(arg)
})
}

type Cache <T> = Record<string, { exp: number, value: T }>

const memoize = <T>(
cache: Cache<T>,
func: Func<any, T>,
keyFunc: Func<string> | null,
ttl: number
) => {
return function callWithMemo(...args: any): T {
const key = keyFunc ? keyFunc(args) : JSON.stringify({ args })
const existing = cache[key]
if (existing !== undefined) {
if (existing.exp > new Date().getTime()) {
return existing.value
}
}
const result = func(...args)
cache[key] = {
exp: new Date().getTime() + ttl,
value: result
}
return result
}
}

export const memo = <TFunc extends Function>(func: TFunc, {
key = null,
ttl = 300
}: {
key?: Func<string> | null
ttl?: number
} = {}) => {
return memoize({}, func as any, key, ttl) as any as TFunc
}
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
partial,
proxied,
partob,
tryit
tryit,
memo
} from './curry'

import {
Expand All @@ -49,7 +50,8 @@ import {
import {
isArray,
isObject,
isFunction
isFunction,
isString
} from './typed'

import {
Expand Down Expand Up @@ -84,6 +86,7 @@ export default {
isArray,
isObject,
isFunction,
isString,
select,
min,
max,
Expand All @@ -92,5 +95,6 @@ export default {
shuffle,
dict,
draw,
random
random,
memo
}
6 changes: 5 additions & 1 deletion src/typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ export const isObject = (value: any) => {

export const isFunction = (value: any) => {
return !!(value && value.constructor && value.call && value.apply)
}
}

export const isString = (val: any) => {
return typeof val === 'string' || val instanceof String
}
69 changes: 7 additions & 62 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,67 +1,12 @@
{
"compilerOptions": {
/* Basic Options */
"incremental": true /* Enable incremental compilation */,
"target": "ES6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "amd" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": ["es2017", "es7", "es6", "dom", "es2020"] /* Specify library files to be included in the compilation. */,
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true /* Generates corresponding '.d.ts' file. */,
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
"sourceMap": true /* Generates corresponding '.map' file. */,
"outDir": "dist" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
"importHelpers": true /* Import emit helpers from 'tslib'. */,
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
"moduleResolution": "node"
"outDir": "dist",
"lib": ["es2016"],
"declaration": true,
"sourceMap": true
},
"include": [
"src/**/*.ts"
],
"exclude": ["node_modules", "dist", "src/tests"]
}

0 comments on commit a930270

Please sign in to comment.