@asod/field 0.1.0-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 {{ TS_PACKAGE_TEMPLATE }}
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/README.md ADDED
@@ -0,0 +1 @@
1
+ # @asod/field
@@ -0,0 +1,25 @@
1
+ import { type IField, type IOperation, type IOperationValue } from './declarations/index';
2
+ type FieldOperationConfig<TValue extends Primitive | IOperationValue> = {
3
+ func: IOperation<TValue>;
4
+ neutralValue: TValue;
5
+ };
6
+ type FieldOperationsConfig<TValue extends Primitive | IOperationValue> = {
7
+ add: FieldOperationConfig<TValue>;
8
+ sub: FieldOperationConfig<TValue>;
9
+ mul: FieldOperationConfig<TValue>;
10
+ div: FieldOperationConfig<TValue>;
11
+ };
12
+ type FieldConfig<TValue extends Primitive | IOperationValue> = {
13
+ operations: FieldOperationsConfig<TValue>;
14
+ };
15
+ declare class Field<TValue extends Primitive | IOperationValue> implements IField<TValue> {
16
+ private readonly _operations;
17
+ constructor(config: FieldConfig<TValue>);
18
+ add(a: TValue, b: TValue): TValue;
19
+ sub(a: TValue, b: TValue): TValue;
20
+ mul(a: TValue, b: TValue): TValue;
21
+ div(a: TValue, b: TValue): TValue;
22
+ private _compare;
23
+ }
24
+ export { Field };
25
+ export default Field;
package/dist/field.js ADDED
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: Object.getOwnPropertyDescriptor(all, name).get
9
+ });
10
+ }
11
+ _export(exports, {
12
+ get Field () {
13
+ return Field;
14
+ },
15
+ get default () {
16
+ return _default;
17
+ }
18
+ });
19
+ const _compare = require("@asod/compare");
20
+ const _guards = require("./utils/guards");
21
+ function _define_property(obj, key, value) {
22
+ if (key in obj) {
23
+ Object.defineProperty(obj, key, {
24
+ value: value,
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true
28
+ });
29
+ } else {
30
+ obj[key] = value;
31
+ }
32
+ return obj;
33
+ }
34
+ class Field {
35
+ add(a, b) {
36
+ const { func, neutralValue } = this._operations.add;
37
+ if (!this._compare(a, neutralValue)) return b;
38
+ if (!this._compare(b, neutralValue)) return a;
39
+ return func(a, b);
40
+ }
41
+ sub(a, b) {
42
+ const { func, neutralValue } = this._operations.sub;
43
+ if (!this._compare(b, neutralValue)) return a;
44
+ return func(a, b);
45
+ }
46
+ mul(a, b) {
47
+ const { func, neutralValue } = this._operations.mul;
48
+ if (!this._compare(a, neutralValue)) return b;
49
+ if (!this._compare(b, neutralValue)) return a;
50
+ return func(a, b);
51
+ }
52
+ div(a, b) {
53
+ const { func, neutralValue } = this._operations.div;
54
+ if (!this._compare(b, neutralValue)) return a;
55
+ return func(a, b);
56
+ }
57
+ _compare(a, b) {
58
+ if ((0, _guards.isComparable)(a)) return (0, _compare.compare)(a, b, a.comparator);
59
+ if ((0, _guards.isComparable)(b)) return (0, _compare.compare)(a, b, b.comparator);
60
+ return (0, _compare.compare)(a, b);
61
+ }
62
+ constructor(config){
63
+ _define_property(this, "_operations", void 0);
64
+ this._operations = config.operations;
65
+ }
66
+ }
67
+ const _default = Field;
@@ -0,0 +1 @@
1
+ type Primitive = string | number | bigint | boolean | symbol | null | undefined;
@@ -0,0 +1,48 @@
1
+ /// <reference path="global.d.ts" />
2
+
3
+ import { type Comparator } from '@asod/compare';
4
+
5
+ declare const ABSTRACT_OPERATION_ID: unique symbol;
6
+
7
+ type AbstractOperationId = typeof ABSTRACT_OPERATION_ID;
8
+
9
+ declare namespace ASOD {
10
+ /* Common */
11
+
12
+ interface IComparable {
13
+ comparator?: Comparator;
14
+ }
15
+
16
+ /* Operation */
17
+
18
+ interface IOperationValue extends IComparable {}
19
+
20
+ interface IOperation<TValue extends Primitive | IOperationValue> {
21
+ (a: TValue, b: TValue): TValue;
22
+ }
23
+
24
+ /* Group */
25
+
26
+ interface IGroup<TValue extends Primitive | IOperationValue> {
27
+ [ABSTRACT_OPERATION_ID]: IOperation<TValue>;
28
+ }
29
+
30
+ /* Ring */
31
+
32
+ // prettier-ignore
33
+ interface IRing<TValue extends Primitive | IOperationValue> extends Omit<IGroup<TValue>, AbstractOperationId> {
34
+ add: IOperation<TValue>;
35
+ mul: IOperation<TValue>;
36
+ }
37
+
38
+ /* Field */
39
+
40
+ // prettier-ignore
41
+ interface IField<TValue extends Primitive | IOperationValue> extends Omit<IRing<TValue>, AbstractOperationId> {
42
+ sub: IOperation<TValue>;
43
+ div: IOperation<TValue>;
44
+ }
45
+ }
46
+
47
+ export = ASOD;
48
+ export as namespace ASOD;
@@ -0,0 +1,2 @@
1
+ import { IComparable, IOperationValue } from '../declarations/index';
2
+ export declare const isComparable: (value: Primitive | IOperationValue) => value is IComparable;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "isComparable", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return isComparable;
9
+ }
10
+ });
11
+ const isComparable = (value)=>typeof value === 'object' && value !== null && 'comparable' in value;
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@asod/field",
3
+ "description": "",
4
+ "version": "0.1.0-canary.0",
5
+ "license": "MIT",
6
+ "author": "Oleg Putseiko <oleg.putseiko@gmail.com> (https://github.com/oleg-putseiko)",
7
+ "keywords": [],
8
+ "homepage": "https://github.com/asodorg/field",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/asodorg/field"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/asodorg/field/issues"
15
+ },
16
+ "main": "dist/index.js",
17
+ "files": [
18
+ "dist/**/*"
19
+ ],
20
+ "exports": "./dist/index.js",
21
+ "scripts": {
22
+ "build": "yarn build:js && yarn build:ts",
23
+ "build:js": "swc src -d dist --strip-leading-paths --ignore \"node_modules,**/*.test.ts,**/*.d.ts\"",
24
+ "build:ts": "tsc && find src -name \"*.d.ts\" -exec cp {} dist/ \\;",
25
+ "lint": "eslint ./src/**/*.ts -c ./eslint.config.mjs",
26
+ "lint:fix": "yarn lint --fix && yarn format:fix",
27
+ "lint:strict": "yarn lint --max-warnings=0 && yarn format",
28
+ "format": "prettier -c .",
29
+ "format:fix": "prettier -w .",
30
+ "typecheck": "tsc --noEmit --incremental false",
31
+ "test": "jest",
32
+ "prepare": "husky install | chmod +x ./.husky/*",
33
+ "version:bump": "npm version -m \"build(package): bump the package version to %s\""
34
+ },
35
+ "devDependencies": {
36
+ "@commitlint/cli": "^18.2.0",
37
+ "@commitlint/config-conventional": "^18.1.0",
38
+ "@swc/cli": "^0.5.2",
39
+ "@swc/core": "^1.10.1",
40
+ "@types/jest": "^30.0.0",
41
+ "@types/node": "^22.10.1",
42
+ "@typescript-eslint/eslint-plugin": "^8.28.0",
43
+ "@typescript-eslint/parser": "^8.28.0",
44
+ "eslint": "^9.16.0",
45
+ "eslint-config-prettier": "^10.1.1",
46
+ "husky": "^8.0.3",
47
+ "jest": "^30.2.0",
48
+ "lint-staged": "^15.0.2",
49
+ "prettier": "^3.0.3",
50
+ "ts-jest": "^29.4.5",
51
+ "ts-node": "^10.9.2",
52
+ "typescript": "^5.7.2"
53
+ },
54
+ "volta": {
55
+ "node": "20.19.5",
56
+ "yarn": "4.10.3"
57
+ },
58
+ "dependencies": {
59
+ "@asod/compare": "^0.1.1-canary.1"
60
+ }
61
+ }