Commit 9d7cf13a authored by nanahira's avatar nanahira

first

parent 8bb3ae8e
Pipeline #15750 passed with stages
in 1 minute and 24 seconds
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
.git*
Dockerfile
.dockerignore
/tests
webpack.config.js
dist/*
build/*
*.js
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
stages:
- install
- build
- deploy
variables:
GIT_DEPTH: "1"
npm_ci:
stage: install
tags:
- linux
script:
- npm ci
artifacts:
paths:
- node_modules
.build_base:
stage: build
tags:
- linux
dependencies:
- npm_ci
build:
extends:
- .build_base
script:
- npm run build
artifacts:
paths:
- dist/
unit-test:
extends:
- .build_base
script:
- npm run test
deploy_npm:
stage: deploy
dependencies:
- build
tags:
- linux
script:
- apt update;apt -y install coreutils
- echo $NPMRC | base64 --decode > ~/.npmrc
- npm publish . --access public && curl -X PUT "https://registry-direct.npmmirror.com/$(cat package.json | jq '.name' | sed 's/\"//g')/sync?sync_upstream=true" || true
only:
- master
/install-npm.sh
.git*
/data
/output
/config.yaml
.idea
.dockerignore
Dockerfile
/src
/coverage
/tests
/dist/tests
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
FROM node:lts-bullseye-slim as base
LABEL Author="Nanahira <nanahira@momobako.com>"
RUN apt update && apt -y install python3 build-essential && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/log/*
WORKDIR /usr/src/app
COPY ./package*.json ./
FROM base as builder
RUN npm ci && npm cache clean --force
COPY . ./
RUN npm run build
FROM base
ENV NODE_ENV production
RUN npm ci && npm cache clean --force
COPY --from=builder /usr/src/app/dist ./dist
CMD [ "npm", "start" ]
The MIT License (MIT)
Copyright (c) 2021 Nanahira
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# onedice.js
OneDice in TypeScript.
\ No newline at end of file
OneDice in TypeScript.
Based on [OneDice](https://github.com/OlivOS-Team/onedice) standard.
## Usage
```ts
import { OneDice } from 'onedice';
const onedice = new OneDice()
onedice.calculate('1d6')
onedice.calculate('2d3+3d4')
```
### Change Random Generator
```ts
class MyOneDice extends OneDice {
random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
}
### Extend operator
const onedice = new OneDice()
onedice.addOperator(new Operator('+', 1, (a, [o, b]) => a + b))
```
\ No newline at end of file
export * from './src/onedice';
export * from './src/operator';
This diff is collapsed.
{
"name": "myproject",
"description": "myproject-desc",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint --fix .",
"build": "rimraf dist && tsc",
"test": "jest --passWithNoTests",
"start": "node dist/index.js"
},
"repository": {
"type": "git",
"url": "https://code.mycard.moe/3rdeye/myproject.git"
},
"author": "Nanahira <nanahira@momobako.com>",
"license": "MIT",
"keywords": [],
"bugs": {
"url": "https://code.mycard.moe/3rdeye/myproject/issues"
},
"homepage": "https://code.mycard.moe/3rdeye/myproject",
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "tests",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"devDependencies": {
"@types/jest": "^28.1.7",
"@types/node": "^18.7.6",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^3.4.1",
"jest": "^28.1.3",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"ts-jest": "^28.0.8",
"typescript": "^4.7.4"
}
}
This diff is collapsed.
type AnyChars<S> = S extends `${infer T}${infer U}` ? T | AnyChars<U> : never;
type OperatorParam<O> = [AnyChars<O>, number];
type OperatorRest<P extends readonly any[]> = P extends []
? []
: P extends readonly [infer O, ...infer R]
? [OperatorParam<O>, ...OperatorRest<R>]
: never;
export class Operator<P extends readonly string[] = readonly string[]> {
constructor(
public sym: P,
public priority: number,
public calculate: (first: number, ...rest: OperatorRest<P>) => number,
) {}
prepareParams(chars: string[], numbers: number[]): OperatorRest<P>;
prepareParams(chars: string[], numbers: number[]) {
const result = this.sym.map((s) => {
const pos = chars.findIndex((char) => s.includes(char));
if (pos === -1) {
return [];
}
return [chars[pos], numbers[pos]];
});
return result;
}
doCalculation(first: number, chars: string[], numbers: number[]) {
return this.calculate(first, ...this.prepareParams(chars, numbers));
}
}
import { OneDice } from '../src/onedice';
class TestOneDice extends OneDice {
random(min: number, max: number): number {
return max;
}
}
describe('Sample test.', () => {
const onedice = new TestOneDice({
valueDict: {
foo: 50,
bar: 60,
},
});
it('should calculate', () => {
expect(onedice.calculate('1+1')).toBe(2);
expect(onedice.calculate('1+2*2')).toBe(5);
expect(onedice.calculate('1+2*(2+3)')).toBe(11);
});
it('should roll dice', () => {
expect(onedice.calculate('1d1')).toBe(1);
expect(onedice.calculate('1d2')).toBe(2);
expect(onedice.calculate('2d3')).toBe(6);
expect(onedice.calculate('2d3+1')).toBe(7);
expect(onedice.calculate('2d3+4d(5*2)')).toBe(46);
expect(onedice.calculate('d6')).toBe(6);
expect(onedice.calculate('d6+d6')).toBe(12);
expect(onedice.calculate('1d+d6+d6')).toBe(18);
});
it('should pass onedice tests', () => {
expect(onedice.calculate('((1-1>2)|(1-1<2))?(1+1):(1-2)')).toBe(2);
expect(onedice.calculate('7d5f')).toBe(35);
});
});
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es2021",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts",
"src/**/*.ts",
"test/**/*.ts",
"tests/**/*.ts"
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment