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

Typical Tyrant #6

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ introspection and organization tools for monorepos

</details>

## typed-packages

* [microplastic](https://github.com/brekk/monoculture/tree/main/typed-packages/microplastic) - weak types seemed good at the time 🥽

<details><summary>Dependencies</summary>

- [ramda](https://www.npmjs.com/package/ramda)
- [remeda](https://www.npmjs.com/package/remeda)
- [@types/ramda](https://www.npmjs.com/package/@types/ramda)🧪
- [dotenv-cli](https://www.npmjs.com/package/dotenv-cli)🧪
- [eslint-config-monoculture](https://github.com/brekk/monoculture/tree/main/shared/eslint-config-monoculture) 🦴🧪
- [jest-config](https://github.com/brekk/monoculture/tree/main/shared/jest-config) 🦴🧪
- [monoculture-tsconfig](https://github.com/brekk/monoculture/tree/main/shared/monoculture-tsconfig) 🦴🧪
- [types-ramda](https://www.npmjs.com/package/types-ramda)🧪
- [typescript](https://www.npmjs.com/package/typescript)🧪

</details>

## packages

* [bloodline](https://github.com/brekk/monoculture/tree/main/packages/bloodline) - determine the relationships between files 🩸
Expand Down
5 changes: 3 additions & 2 deletions package-scripts.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,25 @@ module.exports = {

'apps/docs',
'packages/bloodline',
'packages/knot',
'packages/climate',
'packages/climate-json',
'packages/climate-toml',
'packages/climate-yaml',
'packages/clox',
'packages/file-system',
'packages/knot',
'packages/monocle',
'packages/monorail',
'packages/robot-tourist',
'shared/eslint-config-monoculture',
'shared/jest-config',
'shared/monoculture-tsconfig',
'tools/gitparty',
'tools/doctor-general',
'tools/gitparty',
'tools/spacework',
'tools/superorganism',
'tools/treacle',
'typed-packages/microplastic',
]
),
'sync scripts for all the files'
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"private": true,
"workspaces": [
"apps/*",
"typed-packages/*",
"packages/*",
"shared/*",
"tools/*"
Expand All @@ -13,11 +14,13 @@
"@action-validator/cli": "0.5.4",
"@action-validator/core": "0.5.4",
"@babel/core": "7.23.6",
"@swc/cli": "0.1.63",
"@swc/core": "1.3.101",
"@swc/jest": "0.2.29",
"@swc/register": "0.1.10",
"@types/jest": "29.5.11",
"@types/node": "^20.10.5",
"@types/node": "20.10.5",
"@types/ramda": "^0.29.10",
"dotenv-cli": "7.3.0",
"esbuild": "0.19.10",
"eslint": "8.56.0",
Expand Down
2 changes: 0 additions & 2 deletions shared/monoculture-tsconfig/base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Default",
"compilerOptions": {
"allowJs": true,
"composite": false,
Expand Down
7 changes: 4 additions & 3 deletions shared/monoculture-tsconfig/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"description": "shared tsconfig for monoculture packages 😵",
"version": "0.0.0",
"private": true,
"files": [
"base.json"
],
"exports": {
".": "./base.json",
"./nextjs.json": "./nextjs.json"
},
"scripts": {
"nps": "dotenv -- nps -c ./package-scripts.cjs",
"test": "dotenv -- nps -c ./package-scripts.cjs test",
Expand Down
5 changes: 5 additions & 0 deletions typed-packages/microplastic/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
root: true,
extends: ['monoculture'],
ignorePatterns: ['knot.js'],
}
5 changes: 5 additions & 0 deletions typed-packages/microplastic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# microplastic

![dependencies via madge](./graph.svg)

A sandbox for dealing with the crypt of types
19 changes: 19 additions & 0 deletions typed-packages/microplastic/graph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions typed-packages/microplastic/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const config = require('../../shared/jest-config/jest.config')
module.exports = {
...config,
collectCoverage: true,
coverageReporters: ['html', 'text', 'text-summary', 'cobertura', 'lcov'],
transform: {
...(config.transform || {}),
},
}
32 changes: 32 additions & 0 deletions typed-packages/microplastic/package-scripts.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const buildModule = ([infile, outfile]) =>
[
`esbuild`,
`${infile}`,
`--outfile=${outfile}`,
`--bundle`,
`--format=esm`,
`--platform=node`,
`--packages=external`,
].join(' ')

const INPUT = 'src/index.js'

const sd = (script, description = '') =>
!!description ? { script, description } : { script }
module.exports = {
scripts: {
clean: sd('rm -r dist', 'clean the build'),
build: sd(
'swc ./src/index.ts -o ./dist/microplastic.js',
'add more plastic to the ocean!'
),
lint: sd('eslint --fix .', 'lint!'),
meta: {
graph: `madge ${INPUT} --image graph.svg`,
},
test: {
...sd('jest', 'test!'),
watch: sd('jest --watch', 'test with watch-mode!'),
},
},
}
36 changes: 36 additions & 0 deletions typed-packages/microplastic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "microplastic",
"version": "0.0.0",
"description": "weak types seemed good at the time 🥽",
"type": "module",
"repository": "monoculture",
"author": "brekk",
"license": "ISC",
"private": true,
"exports": {
".": "./dist/microplastic.js"
},
"dependencies": {
"ramda": "0.29.1",
"remeda": "1.33.0"
},
"devDependencies": {
"@types/ramda": "^0.29.9",
"dotenv-cli": "7.3.0",
"eslint-config-monoculture": "workspace:shared/eslint-config-monoculture",
"jest-config": "workspace:shared/jest-config",
"monoculture-tsconfig": "workspace:shared/monoculture-tsconfig",
"types-ramda": "^0.29.6",
"typescript": "5.3.3"
},
"scripts": {
"nps": "dotenv -- nps -c ./package-scripts.cjs",
"build": "dotenv -- nps -c ./package-scripts.cjs build",
"clean": "dotenv -- nps -c ./package-scripts.cjs clean",
"lint": "dotenv -- nps -c ./package-scripts.cjs lint",
"meta": "dotenv -- nps -c ./package-scripts.cjs meta",
"meta:graph": "dotenv -- nps -c ./package-scripts.cjs meta.graph",
"test": "dotenv -- nps -c ./package-scripts.cjs test",
"test:watch": "dotenv -- nps -c ./package-scripts.cjs test.watch"
}
}
21 changes: 21 additions & 0 deletions typed-packages/microplastic/ramda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// so remeda doesn't seem like it actually solves much at first blush

import { curry } from 'ramda'

interface Transformer<F, T> {
(x: F): T
}

interface EqualishBy<F, T> {
(t: Transformer<F, T>, expected: T, x: F): boolean
(t: Transformer<F, T>, expected: T): (x: F) => boolean
(t: Transformer<F, T>): (expected: T, x: F) => boolean
}

export function equalishBy<F, T>(a: Transformer<F, T>, b: T, c: F) {
const raw: EqualishBy<F, T> = curry((transform, expected, x) => {
const t = transform(x)
return t === expected
})
return raw(a, b, c)
}
86 changes: 86 additions & 0 deletions typed-packages/microplastic/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { equalishBy } from './index'
import type { EqualishBy } from './ramda'
import { equalishBy as rBy } from './ramda'

const productIdSelector = (z: string): string => {
const d = z.indexOf('-')
return d > -1 ? z.slice(0, d) : 'NO_MATCH'
}

test('smoke test -- productIdSelector', () => {
expect(productIdSelector('coolco-292929')).toEqual('coolco')
expect(productIdSelector('2020202')).toEqual('NO_MATCH')
})
test('remeda -- equalishBy - no curry', () => {
const isCoolProduct = equalishBy<string, string>(productIdSelector, 'coolco')
// is this the best we can do today?
const isApplicablyCool = (y: string) => equalishBy(productIdSelector, y)
const isCoolProduct2 = isApplicablyCool('coolco')

expect(isCoolProduct('coolco-292929')).toBeTruthy()
expect(isCoolProduct('2020202')).toBeFalsy()

expect(isCoolProduct2('coolco-292929')).toBeTruthy()
expect(isCoolProduct2('2020202')).toBeFalsy()

// this correctly yells about incorrect types
// productIdSelector(/2/g)

expect(equalishBy((a: string): string => a, 'coolco-101')).toBeTruthy()
})

test('remeda -- equalishBy - inline-type', () => {
const isCoolProduct = equalishBy<string, string>(productIdSelector, 'coolco')
// this doesn't work
// const isApplicablyCool: EqualishBy<string, string> =
// equalishBy(productIdSelector)
type FunType = (y: string) => EqualishBy<string, string>
const isApplicablyCool: FunType = equalishBy(productIdSelector)
const isCoolProduct2 = isApplicablyCool('coolco')

expect(isCoolProduct('coolco-292929')).toBeTruthy()
expect(isCoolProduct('2020202')).toBeFalsy()

expect(isCoolProduct2('coolco-292929')).toBeTruthy()
expect(isCoolProduct2('2020202')).toBeFalsy()

expect(equalishBy((a: string): string => a, 'coolco-101')).toBeTruthy()
})

test('remeda -- equalishBy - curry', () => {
const isCoolProduct = equalishBy<string, string>(productIdSelector, 'coolco')
const isApplicablyCool = equalishBy(productIdSelector)
// this fails TS
const isCoolProduct2 = isApplicablyCool('coolco')

expect(isCoolProduct('coolco-292929')).toBeTruthy()
expect(isCoolProduct('2020202')).toBeFalsy()
expect(isCoolProduct2('coolco-292929')).toBeTruthy()
expect(isCoolProduct2('2020202')).toBeFalsy()

// TS hates this
// const yes = equalishBy((a: string): string => a)
// expect(yes('coolco-101')).toBeTruthy()
expect(equalishBy((a: string): string => a, 'coolco-101')).toBeTruthy()
})
test('ramda -- equalishBy', () => {
const selecto = rBy<string, string>(productIdSelector)
const isCoolProduct = selecto('coolco')
expect(isCoolProduct('coolco-292929')).toBeTruthy()
expect(isCoolProduct('2020202')).toBeFalsy()
const isNum = rBy<any, string>(x => typeof x, 'number')
expect(isNum(20000)).toBeTruthy()
expect(isNum('barf')).toBeFalsy()
// currently doesn't barf without overloads
// const hey = rBy<string, string>('test')
})

test('ramda -- equalishBy - undeclared application', () => {
const selecto: EqualishBy<string, string> = rBy(productIdSelector)
const isCoolProduct = selecto('coolco')
expect(isCoolProduct('coolco-292929')).toBeTruthy()
expect(isCoolProduct('2020202')).toBeFalsy()
const isNum: EqualishBy<any, string> = rBy(x => typeof x, 'number')
expect(isNum(20000)).toBeTruthy()
expect(isNum('barf')).toBeFalsy()
})
46 changes: 46 additions & 0 deletions typed-packages/microplastic/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// import * as Q from 'remeda'
import { purry } from 'remeda'

interface Transformer<F, T> {
(arg: F): T
}

/*
this higher-specificy TS version does nothing I can see
export function $equalishBy<F, T>(
x: F,
transform: Transformer<F, T>,
expected: T
): boolean {
// eslint-disable-next-line no-console
console.log({ x, transform, expected })
const t = transform(x)
return t === expected
}
*/

export const $equalishBy = (x, transform, expected) => {
// eslint-disable-next-line no-console
const t = transform(x)
return t === expected
}

export function equalishBy<F, T>(
transform: Transformer<F, T>
): (expected: T, x: F) => boolean

export function equalishBy<F, T>(
transform: Transformer<F, T>,
expected: T
): (x: F) => boolean

export function equalishBy<F, T>(
transform: Transformer<F, T>,
expected: T,
x: F
): boolean

// https://remedajs.com/docs#purry
export function equalishBy() {
return purry($equalishBy, arguments)
}
16 changes: 16 additions & 0 deletions typed-packages/microplastic/src/ramda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { curry } from 'ramda'

interface Transformer<F, T> {
(arg: F): T
}

export interface EqualishBy<F, T> {
(transform: Transformer<F, T>, expected: T, x: F): boolean
(transform: Transformer<F, T>, expected: T): (x: F) => boolean
(transform: Transformer<F, T>): (expected: T, x: F) => boolean
}

export const equalishBy = curry(
<F, T>(transform: Transformer<F, T>, expected: T, x: F) =>
transform(x) === expected
)
4 changes: 4 additions & 0 deletions typed-packages/microplastic/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../shared/monoculture-tsconfig/base.json",
"include": ["./microplastic.ts"]
}
Loading
Loading