@mark1russell7/market 0.0.0-stage → 0.1.2
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 +21 -0
- package/README.md +109 -2
- package/build/index.d.ts +424 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +620 -0
- package/build/index.js.map +1 -0
- package/package.json +61 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mark Russell
|
|
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
CHANGED
|
@@ -1,3 +1,110 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mark1russell7/market
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The records of a market: a product, an offer of a seller and a price reading, with their checks. The package also has the GTIN check, the match keys of a record, the cost of an order and the storage rules of a source. The offers of a part and the best offer join the observations of the sources to the parts that a buyer wants. The board keeps the current prices within the terms of each source, and plans the reads of a watch.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pnpm add @mark1russell7/market
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
The records come from the hand crawl ([`docs/HAND-CRAWL.md`](../../docs/HAND-CRAWL.md)). The fields are the common fields of the official APIs of the sellers.
|
|
10
|
+
|
|
11
|
+
## The records
|
|
12
|
+
|
|
13
|
+
Each record has a `zod` schema. A schema refuses an unknown field, an empty text and a text with a space at an end. An adapter of a source gives records in their final form, and the schema examines them.
|
|
14
|
+
|
|
15
|
+
| Record | Schema | What it has |
|
|
16
|
+
| --- | --- | --- |
|
|
17
|
+
| Product | `productSchema` | An id of the catalog, a title, a brand, a category path, GTINs, part numbers and specifications as text. |
|
|
18
|
+
| Offer | `offerSchema` | One product at one seller: the source, the id at the source, the seller, the condition, the buying format and the identifiers that the source gives. |
|
|
19
|
+
| Price reading | `priceReadingSchema` | The state of one offer at one instant: the unit price, the list price, the shipping, the availability, the minimum quantity and the price breaks. |
|
|
20
|
+
| Terms of a source | `sourceTermsSchema` | The storage rule of the source, the deletion of ended offers, and the permission of a model of prices. |
|
|
21
|
+
|
|
22
|
+
- **Money.** An amount of money is a quantity of [`@mark1russell7/quantity`](https://www.npmjs.com/package/@mark1russell7/quantity), for example `{ "value": 64.99, "unit": "USD" }`. A price is zero or more.
|
|
23
|
+
- **Instants.** An instant is an ISO 8601 text in UTC, for example `2026-11-12T18:00:00Z`.
|
|
24
|
+
- **Ids.** The id of an offer is the name of its source, a colon and the id at the source: `offerId("ebay", "1234")` gives `ebay:1234`. A source name has small letters, digits and hyphens.
|
|
25
|
+
- **Conditions.** A source maps its own condition to `new`, `open-box`, `refurbished`, `used`, `for-parts` or `unknown`. The field `grade` keeps the text of the source, for example `excellent`.
|
|
26
|
+
- **Shipping.** The shipping is `free`, `fixed` with a cost, `calculated` (it depends on the address) or `unknown`.
|
|
27
|
+
- **Price breaks.** `price` is the unit price for an order of `minimumQuantity` units, or of one unit. The quantities of the breaks increase, and each is more than the minimum.
|
|
28
|
+
- **Stock.** A count of `available` units agrees with the availability: `out-of-stock` has the count 0, and `in-stock` and `low-stock` have a count of 1 or more.
|
|
29
|
+
|
|
30
|
+
## The functions
|
|
31
|
+
|
|
32
|
+
- `parseGtin(text)` gives the GTIN-14 form of a GTIN-8, a UPC-A, an EAN-13 or a GTIN-14, or null. It examines the check digit, and it refuses a GTIN of zeros only.
|
|
33
|
+
- `matchKeys({ gtins, partNumbers })` gives the keys of a record. A key is `gtin:` with the GTIN-14, or `part:` with the folded brand and part number. Two records with a key in common name one product.
|
|
34
|
+
- `unitPriceFor(reading, count)` gives the unit price for an order of `count` units. `costOf(reading, count)` adds the shipping, or it gives null when the shipping cost is not known. The cost is exact and not rounded.
|
|
35
|
+
- `keepUntil(terms, reading)` gives the last instant at which a store keeps a reading, or null with no limit. `isKept(terms, reading, now, ended)` tells if a store keeps it at `now`.
|
|
36
|
+
- `permitsHistory(terms)` and `permitsTrend(terms)` tell if the terms of a source permit a stored history and a trend of its prices.
|
|
37
|
+
|
|
38
|
+
## The offers of a part
|
|
39
|
+
|
|
40
|
+
An observation is one offer and its price reading at one instant. The parsers of `@mark1russell7/market-sources` give observations.
|
|
41
|
+
|
|
42
|
+
- `offersPart(offer, want)` tells if an offer is an offer of a part. A want has the names of the maker and the part numbers of the maker. One part number of the offer must have a brand of the want and start with a part number of the want, after the fold.
|
|
43
|
+
- `offersOfPart(observations, want)` gives the latest observation of each offer of the part. `latestObservations(observations)` does the same for each offer.
|
|
44
|
+
- `bestOffer(observations, { currency, count, conditions })` chooses the offer to buy. It skips an offer with another condition, another currency, or a minimum above the count. Each skipped offer has its reason. The offers go from the lowest price of the goods, then the lowest cost with the shipping. The best offer is the first offer in stock, or else the first offer.
|
|
45
|
+
|
|
46
|
+
The brand is necessary, because many makers use short part numbers. A search for the number of a power bank found a solder tip and a fan of other makers.
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import { bestOffer, offersOfPart, type PartWant } from "@mark1russell7/market";
|
|
50
|
+
|
|
51
|
+
const want: PartWant = { brands: ["Acme", "Acme Power Inc."], partNumbers: ["UPS-600X"] };
|
|
52
|
+
const chosen = bestOffer(offersOfPart(observations, want), { currency: "USD" });
|
|
53
|
+
chosen.best?.goods; // { value: 89.99, unit: "USD" }
|
|
54
|
+
chosen.skipped; // [{ offerId: "ebay:1234", why: "The condition used is not a condition that the buyer accepts." }]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## The board of current prices
|
|
58
|
+
|
|
59
|
+
A watch reads its sources on a schedule. A board keeps the last read of each entry of the watch, and the terms of each source limit it. The functions of the board are calculations with no effect. A host does the reads, and it writes the board.
|
|
60
|
+
|
|
61
|
+
- `settleBoard(board, { terms, now, names }, read)` gives the new board. A new read replaces the last read of its entry, so an offer that the new read does not give leaves the board. Then the terms apply to each read. The board also drops the read of an entry that the watch does not have.
|
|
62
|
+
- `boardObservations(board, { terms, now })` gives the current observations of a board: the latest observation of each offer over the reads, within the terms.
|
|
63
|
+
- `keepObservations(observations, { terms, now, ended })` gives the latest observation of each offer that a store keeps at `now`, and each dropped offer with its reason. The board drops a reading of a source with `no-store` or with no terms. It also drops a reading past its limit, and an ended offer.
|
|
64
|
+
- `readInterval(entry, { terms, slack })` gives the time from one read of a watch entry to the next. It is the shorter of `every` and the limit of the terms less the slack of the loop. Thus the board has a current reading of the entry at each instant after its first read.
|
|
65
|
+
- `watchProblems(entries, { terms, slack })` gives each entry with no interval, and each name that two entries have. A source with `no-store` has no interval: a program reads it for each request.
|
|
66
|
+
- `planReads(entries, last, now, { terms, slack })` gives the due entries at `now`, and the instant of the next due read. `lastReads(board)` gives `last`.
|
|
67
|
+
- `boardSchema`, `boardReadSchema`, `observationSchema` and `watchEntrySchema` examine a board file, a read, an observation and a watch entry.
|
|
68
|
+
|
|
69
|
+
Settle the board after each read, and also when no read is due: a reading can pass its limit between two reads. Write each new board in place of the old board.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { bestOffer, boardObservations, lastReads, offersOfPart, planReads, settleBoard } from "@mark1russell7/market";
|
|
73
|
+
|
|
74
|
+
const plan = planReads(entries, lastReads(board), now, { terms, slack: { value: 2, unit: "min" } });
|
|
75
|
+
// Read each entry of plan.due. Then settle the board with each read, and write the board.
|
|
76
|
+
board = settleBoard(board, { terms, now, names }, { name: "ups", at: now, observations }).board;
|
|
77
|
+
const chosen = bestOffer(offersOfPart(boardObservations(board, { terms, now }), want), { currency: "USD" });
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Example
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { costOf, isKept, priceReadingSchema, type SourceTerms } from "@mark1russell7/market";
|
|
84
|
+
|
|
85
|
+
const reading = priceReadingSchema.parse({
|
|
86
|
+
offerId: "example:sku-1",
|
|
87
|
+
at: "2026-11-12T18:00:00Z",
|
|
88
|
+
price: { value: 0.1, unit: "USD" },
|
|
89
|
+
shipping: { kind: "fixed", cost: { value: 0.2, unit: "USD" } },
|
|
90
|
+
availability: "in-stock",
|
|
91
|
+
});
|
|
92
|
+
costOf(reading, 3); // { value: 0.5, unit: "USD" }
|
|
93
|
+
|
|
94
|
+
const terms: SourceTerms = {
|
|
95
|
+
source: "example",
|
|
96
|
+
retention: { kind: "max-age", age: { value: 72, unit: "h" } },
|
|
97
|
+
deleteWhenEnded: false,
|
|
98
|
+
priceModel: false,
|
|
99
|
+
clause: "a cache of 72 hours at most",
|
|
100
|
+
};
|
|
101
|
+
isKept(terms, reading, "2026-11-16T00:00:00Z"); // false
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Limits
|
|
105
|
+
|
|
106
|
+
- The match keys do not know the aliases of a brand. `Western Digital` and `WD` give two keys. A want of a part names each form of the brand.
|
|
107
|
+
- A part number of a want matches each longer part number that starts with it. A want gives the full part number of the maker.
|
|
108
|
+
- The money of the quantity package has one currency: the US dollar.
|
|
109
|
+
- The package has no history of prices yet. Roadmap item R5 adds it, after the owner chooses its source.
|
|
110
|
+
- The board does not know if an offer ended when a new read does not give it. It drops the offer from that read. Another read can still give it.
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The records of the market: a product, an offer of a seller and a price reading, with their checks. The module
|
|
3
|
+
* also has the GTIN check, the match keys of a record, the cost of an order and the storage rules of a source.
|
|
4
|
+
* The offers of a part and the best offer join the observations of the sources to the parts that a buyer wants.
|
|
5
|
+
* The board keeps the current prices within the terms of each source, and plans the reads of a watch.
|
|
6
|
+
*/
|
|
7
|
+
import { Quantity } from "@mark1russell7/quantity";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
/** A part number of a maker: the brand, and the number that the maker gives to the part. */
|
|
10
|
+
interface PartNumber {
|
|
11
|
+
readonly brand: string;
|
|
12
|
+
readonly partNumber: string;
|
|
13
|
+
}
|
|
14
|
+
/** The identifiers of a product or of an offer. */
|
|
15
|
+
interface Identifiers {
|
|
16
|
+
readonly gtins: readonly string[];
|
|
17
|
+
readonly partNumbers: readonly PartNumber[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* This function folds a brand or a part number for a comparison. The result has only capital letters A to Z
|
|
21
|
+
* and digits. Thus `MZ-V8V1T0B/AM` and `mzv8v1t0bam` fold to one text.
|
|
22
|
+
*/
|
|
23
|
+
export declare function foldCode(text: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* This function gives the match keys of a record, sorted and with no copy. A key is `gtin:` with the GTIN-14,
|
|
26
|
+
* or `part:` with the folded brand and part number. Two records with a key in common name one product.
|
|
27
|
+
* The function ignores a GTIN that is not valid, and a part number whose brand or number folds to no text.
|
|
28
|
+
*/
|
|
29
|
+
export declare function matchKeys(identifiers: Identifiers): string[];
|
|
30
|
+
/**
|
|
31
|
+
* The conditions of an offer. A source maps its own condition to one of them, and it keeps its own text in
|
|
32
|
+
* the field `grade` of the offer.
|
|
33
|
+
*/
|
|
34
|
+
export declare const CONDITIONS: readonly ["new", "open-box", "refurbished", "used", "for-parts", "unknown"];
|
|
35
|
+
/** The condition of an offer. */
|
|
36
|
+
type Condition = (typeof CONDITIONS)[number];
|
|
37
|
+
/** The states of the stock of an offer at the time of a reading. */
|
|
38
|
+
export declare const AVAILABILITIES: readonly ["in-stock", "low-stock", "out-of-stock", "preorder", "unknown"];
|
|
39
|
+
/** The state of the stock of an offer. */
|
|
40
|
+
type Availability = (typeof AVAILABILITIES)[number];
|
|
41
|
+
/** The buying formats of an offer. In an auction, the price of a reading is the current bid. */
|
|
42
|
+
export declare const FORMATS: readonly ["fixed-price", "auction"];
|
|
43
|
+
/** The buying format of an offer. */
|
|
44
|
+
type Format = (typeof FORMATS)[number];
|
|
45
|
+
/** One specification of a product as text. */
|
|
46
|
+
interface Spec {
|
|
47
|
+
readonly name: string;
|
|
48
|
+
readonly value: string;
|
|
49
|
+
}
|
|
50
|
+
/** The feedback of a seller: the percent of good feedback, and the count of the feedback. */
|
|
51
|
+
interface Rating {
|
|
52
|
+
readonly percent: number;
|
|
53
|
+
readonly count: number;
|
|
54
|
+
}
|
|
55
|
+
/** A seller of an offer. A source gives some of these fields, or none. */
|
|
56
|
+
interface Seller {
|
|
57
|
+
readonly id?: string | undefined;
|
|
58
|
+
readonly name?: string | undefined;
|
|
59
|
+
readonly rating?: Rating | undefined;
|
|
60
|
+
}
|
|
61
|
+
/** A product of the catalog: one thing that a person can buy, with its identifiers and its specifications. */
|
|
62
|
+
interface Product {
|
|
63
|
+
readonly id: string;
|
|
64
|
+
readonly title: string;
|
|
65
|
+
readonly brand?: string | undefined;
|
|
66
|
+
readonly category?: readonly string[] | undefined;
|
|
67
|
+
readonly gtins: readonly string[];
|
|
68
|
+
readonly partNumbers: readonly PartNumber[];
|
|
69
|
+
readonly specs: readonly Spec[];
|
|
70
|
+
}
|
|
71
|
+
/** An offer of a seller: one product at one seller, with the identifiers that the source gives. */
|
|
72
|
+
interface Offer {
|
|
73
|
+
readonly id: string;
|
|
74
|
+
readonly source: string;
|
|
75
|
+
readonly sourceId: string;
|
|
76
|
+
readonly productId?: string | undefined;
|
|
77
|
+
readonly title: string;
|
|
78
|
+
readonly url?: string | undefined;
|
|
79
|
+
readonly seller: Seller;
|
|
80
|
+
readonly condition: Condition;
|
|
81
|
+
readonly grade?: string | undefined;
|
|
82
|
+
readonly format: Format;
|
|
83
|
+
readonly endsAt?: string | undefined;
|
|
84
|
+
readonly gtins: readonly string[];
|
|
85
|
+
readonly partNumbers: readonly PartNumber[];
|
|
86
|
+
readonly specs: readonly Spec[];
|
|
87
|
+
}
|
|
88
|
+
/** The shipping of a reading. A calculated cost depends on the address of the buyer. */
|
|
89
|
+
type Shipping = {
|
|
90
|
+
readonly kind: "free";
|
|
91
|
+
} | {
|
|
92
|
+
readonly kind: "fixed";
|
|
93
|
+
readonly cost: Quantity;
|
|
94
|
+
} | {
|
|
95
|
+
readonly kind: "calculated";
|
|
96
|
+
} | {
|
|
97
|
+
readonly kind: "unknown";
|
|
98
|
+
};
|
|
99
|
+
/** A price break of a reading: the unit price for an order of `quantity` units or more. */
|
|
100
|
+
interface PriceBreak {
|
|
101
|
+
readonly quantity: number;
|
|
102
|
+
readonly unitPrice: Quantity;
|
|
103
|
+
}
|
|
104
|
+
/** A price reading: the state of one offer at one instant. */
|
|
105
|
+
interface PriceReading {
|
|
106
|
+
readonly offerId: string;
|
|
107
|
+
readonly at: string;
|
|
108
|
+
readonly price: Quantity;
|
|
109
|
+
readonly listPrice?: Quantity | undefined;
|
|
110
|
+
readonly shipping: Shipping;
|
|
111
|
+
readonly availability: Availability;
|
|
112
|
+
readonly available?: number | undefined;
|
|
113
|
+
readonly minimumQuantity?: number | undefined;
|
|
114
|
+
readonly priceBreaks?: readonly PriceBreak[] | undefined;
|
|
115
|
+
}
|
|
116
|
+
/** This schema accepts a text with a character that is not a space, and with no space at an end. */
|
|
117
|
+
export declare const textSchema: z.ZodType<string>;
|
|
118
|
+
/** This schema accepts the name of a source: small letters, digits and hyphens, with a letter first. */
|
|
119
|
+
export declare const sourceSchema: z.ZodType<string>;
|
|
120
|
+
/** This schema accepts a GTIN in its GTIN-14 form, with a correct check digit. `parseGtin` gives this form. */
|
|
121
|
+
export declare const gtinSchema: z.ZodType<string>;
|
|
122
|
+
/** This schema accepts an amount of money of zero or more, for example `{ "value": 129.99, "unit": "USD" }`. */
|
|
123
|
+
export declare const moneySchema: z.ZodType<Quantity, Quantity>;
|
|
124
|
+
/** This schema accepts an instant in UTC in the ISO 8601 form, for example `2026-11-12T18:00:00Z`. */
|
|
125
|
+
export declare const instantSchema: z.ZodType<string>;
|
|
126
|
+
/** This schema accepts a part number of a maker. */
|
|
127
|
+
export declare const partNumberSchema: z.ZodType<PartNumber>;
|
|
128
|
+
/** This schema accepts one specification as text, for example `{ "name": "Capacity", "value": "1 TB" }`. */
|
|
129
|
+
export declare const specSchema: z.ZodType<Spec>;
|
|
130
|
+
/** This schema accepts a seller. A rating is the percent of good feedback and the count of the feedback. */
|
|
131
|
+
export declare const sellerSchema: z.ZodType<Seller>;
|
|
132
|
+
/** This schema accepts a product: one thing that a person can buy, with its identifiers and its specifications. */
|
|
133
|
+
export declare const productSchema: z.ZodType<Product>;
|
|
134
|
+
/** This function gives the id of an offer: the name of its source, a colon, and the id at the source. */
|
|
135
|
+
export declare function offerId(source: string, sourceId: string): string;
|
|
136
|
+
/** This function gives the name of the source of an offer id: the text before the first colon. */
|
|
137
|
+
export declare function sourceOfOffer(id: string): string;
|
|
138
|
+
/**
|
|
139
|
+
* This schema accepts an offer: one product at one seller. The offer has the identifiers that the source gives.
|
|
140
|
+
* `productId` names the product of the catalog after a match.
|
|
141
|
+
*/
|
|
142
|
+
export declare const offerSchema: z.ZodType<Offer>;
|
|
143
|
+
/** This schema accepts the shipping of a reading. A calculated cost depends on the address of the buyer. */
|
|
144
|
+
export declare const shippingSchema: z.ZodType<Shipping>;
|
|
145
|
+
/** This schema accepts a price break: the unit price for an order of `quantity` units or more. */
|
|
146
|
+
export declare const priceBreakSchema: z.ZodType<PriceBreak>;
|
|
147
|
+
/**
|
|
148
|
+
* This schema accepts a price reading: the state of one offer at one instant. `price` is the unit price for an
|
|
149
|
+
* order of `minimumQuantity` units, or of one unit. The quantities of the breaks increase, and each is more
|
|
150
|
+
* than the minimum.
|
|
151
|
+
*/
|
|
152
|
+
export declare const priceReadingSchema: z.ZodType<PriceReading>;
|
|
153
|
+
/** One offer of a source, and its price reading at one instant. */
|
|
154
|
+
interface Observation {
|
|
155
|
+
readonly offer: Offer;
|
|
156
|
+
readonly reading: PriceReading;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* This record names a part that a buyer wants: the names of its maker, and its part numbers. A seller can write
|
|
160
|
+
* the brand of one maker in more than one form, so the want has a list of names.
|
|
161
|
+
*/
|
|
162
|
+
interface PartWant {
|
|
163
|
+
/** The names of the maker, as the sellers write the brand. */
|
|
164
|
+
readonly brands: readonly string[];
|
|
165
|
+
/** The part numbers of the maker. A part number of an offer matches when it starts with one of them, after the fold. */
|
|
166
|
+
readonly partNumbers: readonly string[];
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* This function tells if an offer is an offer of the part. One part number of the offer must have a brand of the
|
|
170
|
+
* want and start with a part number of the want. `foldCode` folds each text before the comparison. A part number
|
|
171
|
+
* of another maker does not match: many makers use short numbers like `A1383`.
|
|
172
|
+
*/
|
|
173
|
+
export declare function offersPart(offer: Pick<Offer, "partNumbers">, want: PartWant): boolean;
|
|
174
|
+
/**
|
|
175
|
+
* This function gives the latest observation of each offer, in the order of the offer ids. When two readings of
|
|
176
|
+
* one offer have the same instant, the later one in the list applies.
|
|
177
|
+
*/
|
|
178
|
+
export declare function latestObservations(observations: readonly Observation[]): Observation[];
|
|
179
|
+
/** This function gives the latest observation of each offer of the part, in the order of the offer ids. */
|
|
180
|
+
export declare function offersOfPart(observations: readonly Observation[], want: PartWant): Observation[];
|
|
181
|
+
/** This record is one offer that a buyer can choose, with the price of an order in the currency of the buyer. */
|
|
182
|
+
interface OfferChoice {
|
|
183
|
+
readonly observation: Observation;
|
|
184
|
+
/** The unit price for the count, times the count. */
|
|
185
|
+
readonly goods: Quantity;
|
|
186
|
+
/** The goods and the shipping, or null when the shipping cost is not known. */
|
|
187
|
+
readonly cost: Quantity | null;
|
|
188
|
+
/** True when the availability is `in-stock` or `low-stock`. */
|
|
189
|
+
readonly inStock: boolean;
|
|
190
|
+
}
|
|
191
|
+
/** These options change the choice of an offer. */
|
|
192
|
+
interface BestOfferOptions {
|
|
193
|
+
/** The currency of the prices, a unit of money of `@mark1russell7/quantity`, for example `USD`. An offer with a price in another currency is skipped. */
|
|
194
|
+
readonly currency: string;
|
|
195
|
+
/** The count of units of the order. The default is 1. */
|
|
196
|
+
readonly count?: number;
|
|
197
|
+
/** The conditions that the buyer accepts. The default is `new` only. */
|
|
198
|
+
readonly conditions?: readonly Condition[];
|
|
199
|
+
}
|
|
200
|
+
/** This record is the choice of an offer: the best offer, each offer in the order of the price, and each skipped offer. */
|
|
201
|
+
interface BestOffer {
|
|
202
|
+
/** The offer with the lowest price that is in stock, or else the offer with the lowest price, or null with no offer. */
|
|
203
|
+
readonly best: OfferChoice | null;
|
|
204
|
+
/** The offers, from the lowest price of the goods. Equal prices go in the order of the cost, then of the offer id. */
|
|
205
|
+
readonly offers: readonly OfferChoice[];
|
|
206
|
+
/** The offers that the choice skipped, each with the reason. */
|
|
207
|
+
readonly skipped: readonly {
|
|
208
|
+
readonly offerId: string;
|
|
209
|
+
readonly why: string;
|
|
210
|
+
}[];
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* This function chooses the offer to buy from the latest observation of each offer. It skips an offer with a
|
|
214
|
+
* condition that the buyer does not accept, a price in another currency, or a minimum above the count.
|
|
215
|
+
*
|
|
216
|
+
* @throws RangeError when the count is not a whole number of 1 or more, or when the currency is not a unit of money.
|
|
217
|
+
*/
|
|
218
|
+
export declare function bestOffer(observations: readonly Observation[], options: BestOfferOptions): BestOffer;
|
|
219
|
+
/** The storage rule of a source, from its terms. */
|
|
220
|
+
type Retention = {
|
|
221
|
+
readonly kind: "keep";
|
|
222
|
+
} | {
|
|
223
|
+
readonly kind: "max-age";
|
|
224
|
+
readonly age: Quantity;
|
|
225
|
+
} | {
|
|
226
|
+
readonly kind: "no-store";
|
|
227
|
+
};
|
|
228
|
+
/** The rules of the terms of one source. */
|
|
229
|
+
interface SourceTerms {
|
|
230
|
+
readonly source: string;
|
|
231
|
+
readonly retention: Retention;
|
|
232
|
+
readonly deleteWhenEnded: boolean;
|
|
233
|
+
readonly priceModel: boolean;
|
|
234
|
+
readonly clause: string;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* This schema accepts the storage rule of a source, from its terms. With `keep`, the terms set no limit on the
|
|
238
|
+
* time. With `max-age`, a store keeps a reading for `age` at most. With `no-store`, a reading serves one request.
|
|
239
|
+
*/
|
|
240
|
+
export declare const retentionSchema: z.ZodType<Retention>;
|
|
241
|
+
/**
|
|
242
|
+
* This schema accepts the rules of the terms of one source. `deleteWhenEnded` deletes the readings of an offer
|
|
243
|
+
* when the offer ends. `priceModel` permits a trend or a forecast of prices. `clause` names the text of the terms.
|
|
244
|
+
*/
|
|
245
|
+
export declare const sourceTermsSchema: z.ZodType<SourceTerms>;
|
|
246
|
+
/**
|
|
247
|
+
* This function gives the last instant at which a store keeps a reading, or null when the terms set no
|
|
248
|
+
* limit. For a source with `no-store`, the instant is the instant of the reading.
|
|
249
|
+
*/
|
|
250
|
+
export declare function keepUntil(terms: SourceTerms, reading: {
|
|
251
|
+
readonly at: string;
|
|
252
|
+
}): string | null;
|
|
253
|
+
/**
|
|
254
|
+
* This function tells if a store keeps a reading at the instant `now`. A store keeps no reading of a source with
|
|
255
|
+
* `no-store`, and no reading of an ended offer when the terms delete them.
|
|
256
|
+
*/
|
|
257
|
+
export declare function isKept(terms: SourceTerms, reading: {
|
|
258
|
+
readonly at: string;
|
|
259
|
+
}, now: string, ended?: boolean): boolean;
|
|
260
|
+
/** This function tells if the terms permit a stored history of the readings of a source. */
|
|
261
|
+
export declare function permitsHistory(terms: SourceTerms): boolean;
|
|
262
|
+
/** This function tells if the terms permit a trend of the prices of a source: a history and a model of prices. */
|
|
263
|
+
export declare function permitsTrend(terms: SourceTerms): boolean;
|
|
264
|
+
/** The format name of a board record. */
|
|
265
|
+
export declare const BOARD_FORMAT = "price-board/1";
|
|
266
|
+
/** This schema accepts an observation: an offer, and a reading of that offer. */
|
|
267
|
+
export declare const observationSchema: z.ZodType<Observation>;
|
|
268
|
+
/** This record is the last read of one entry of a watch: the name of the entry, the instant, and the observations. */
|
|
269
|
+
interface BoardRead {
|
|
270
|
+
readonly name: string;
|
|
271
|
+
readonly at: string;
|
|
272
|
+
readonly observations: readonly Observation[];
|
|
273
|
+
}
|
|
274
|
+
/** This record is a board: the instant of its last settle, and the last read of each entry, in the order of the names. */
|
|
275
|
+
interface Board {
|
|
276
|
+
readonly format: typeof BOARD_FORMAT;
|
|
277
|
+
readonly at: string;
|
|
278
|
+
readonly reads: readonly BoardRead[];
|
|
279
|
+
}
|
|
280
|
+
/** This schema accepts the read of an entry. The read has one observation of each offer at most. */
|
|
281
|
+
export declare const boardReadSchema: z.ZodType<BoardRead>;
|
|
282
|
+
/** This schema accepts a board. The board has one read of each entry at most. */
|
|
283
|
+
export declare const boardSchema: z.ZodType<Board>;
|
|
284
|
+
/** This record is an offer that the store does not keep, with the reason. */
|
|
285
|
+
interface Dropped {
|
|
286
|
+
readonly offerId: string;
|
|
287
|
+
readonly why: string;
|
|
288
|
+
}
|
|
289
|
+
/** These options give the rules of a store: the terms of each source, the instant, and the offers that ended. */
|
|
290
|
+
interface KeepOptions {
|
|
291
|
+
/** The terms of the sources. A store keeps no reading of a source without terms. */
|
|
292
|
+
readonly terms: readonly SourceTerms[];
|
|
293
|
+
/** The instant of the decision. */
|
|
294
|
+
readonly now: string;
|
|
295
|
+
/** The ids of the offers that ended, as their sources tell. An offer with `endsAt` at or before `now` also ended. */
|
|
296
|
+
readonly ended?: readonly string[] | undefined;
|
|
297
|
+
}
|
|
298
|
+
/** This record is the decision of a store: the observations that it keeps, and each offer that it drops. */
|
|
299
|
+
interface Kept {
|
|
300
|
+
/** The latest observation of each kept offer, in the order of the offer ids. */
|
|
301
|
+
readonly kept: readonly Observation[];
|
|
302
|
+
/** Each dropped offer, in the order of the offer ids. */
|
|
303
|
+
readonly dropped: readonly Dropped[];
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* This function decides which observations a store keeps. It takes the latest observation of each offer. It keeps
|
|
307
|
+
* an observation while the terms of its source permit it, and while its offer has not ended. A current price is
|
|
308
|
+
* a price that a buyer can pay, so the store drops an ended offer for each source.
|
|
309
|
+
*
|
|
310
|
+
* `settleBoard` applies this function to each read of a board.
|
|
311
|
+
*
|
|
312
|
+
* @throws RangeError when `now` is not an instant, or when the terms have two entries for one source.
|
|
313
|
+
*/
|
|
314
|
+
export declare function keepObservations(observations: readonly Observation[], options: KeepOptions): Kept;
|
|
315
|
+
/** These options give the rules of a settle: the rules of a store, and the names of the entries of the watch. */
|
|
316
|
+
interface SettleOptions extends KeepOptions {
|
|
317
|
+
/** The names of the entries of the watch. The board drops the read of each other name. With no list, it keeps each read. */
|
|
318
|
+
readonly names?: readonly string[] | undefined;
|
|
319
|
+
}
|
|
320
|
+
/** This record is an offer that the board drops, with the name of the entry of its read. */
|
|
321
|
+
interface BoardDrop extends Dropped {
|
|
322
|
+
readonly entry: string;
|
|
323
|
+
}
|
|
324
|
+
/** This record is the result of a settle: the new board, and each offer that it dropped. */
|
|
325
|
+
interface Settled {
|
|
326
|
+
readonly board: Board;
|
|
327
|
+
readonly dropped: readonly BoardDrop[];
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* This function settles a board at `now`. A new read replaces the last read of its entry, so an offer that the new
|
|
331
|
+
* read does not give leaves the read. Then `keepObservations` applies the terms to each read. The board drops the
|
|
332
|
+
* read of an entry that the watch does not have. Write the new board in place of the old board.
|
|
333
|
+
*
|
|
334
|
+
* @param read - A new read of an entry, or none to settle the board alone
|
|
335
|
+
* @throws RangeError when `now` is not an instant, or when the terms have two entries for one source.
|
|
336
|
+
*/
|
|
337
|
+
export declare function settleBoard(board: Board, options: SettleOptions, read?: BoardRead): Settled;
|
|
338
|
+
/**
|
|
339
|
+
* This function gives the current observations of a board at `now`. Each is the latest observation of an offer over
|
|
340
|
+
* the reads, while the terms of its source permit it. Give them to `offersOfPart` and `bestOffer`.
|
|
341
|
+
*/
|
|
342
|
+
export declare function boardObservations(board: Board, options: KeepOptions): Observation[];
|
|
343
|
+
/** This function gives the instant of the last read of each entry of a board, by name, for `planReads`. */
|
|
344
|
+
export declare function lastReads(board: Board): Record<string, string>;
|
|
345
|
+
/** This record is one entry of a watch: a read of one source that repeats. */
|
|
346
|
+
interface WatchEntry {
|
|
347
|
+
/** The name of the entry. Each entry of a watch has its own name. */
|
|
348
|
+
readonly name: string;
|
|
349
|
+
/** The source of the read. */
|
|
350
|
+
readonly source: string;
|
|
351
|
+
/** The time from one read of the entry to the next, a time of `@mark1russell7/quantity`. */
|
|
352
|
+
readonly every: Quantity;
|
|
353
|
+
}
|
|
354
|
+
/** This schema accepts an entry of a watch. */
|
|
355
|
+
export declare const watchEntrySchema: z.ZodType<WatchEntry>;
|
|
356
|
+
/** These options give the rules of a schedule: the terms of each source, and the slack of the loop. */
|
|
357
|
+
interface ScheduleOptions {
|
|
358
|
+
/** The terms of the sources. */
|
|
359
|
+
readonly terms: readonly SourceTerms[];
|
|
360
|
+
/**
|
|
361
|
+
* The longest time from the instant of a due read to the instant that its reading is on the board. It is the
|
|
362
|
+
* time between two passes of the loop, plus the longest pass.
|
|
363
|
+
*/
|
|
364
|
+
readonly slack: Quantity;
|
|
365
|
+
}
|
|
366
|
+
/** This record is the interval of an entry, in milliseconds, or the reason that the entry has no interval. */
|
|
367
|
+
type ReadInterval = {
|
|
368
|
+
readonly ok: true;
|
|
369
|
+
readonly ms: number;
|
|
370
|
+
} | {
|
|
371
|
+
readonly ok: false;
|
|
372
|
+
readonly why: string;
|
|
373
|
+
};
|
|
374
|
+
/**
|
|
375
|
+
* This function gives the time from one read of an entry to the next, in milliseconds. A reading of a source with
|
|
376
|
+
* the limit `L` stays on the board until `L` after the read. The next reading arrives `slack` after the next read
|
|
377
|
+
* is due, at most. Thus the interval is the shorter of `every` and `L - slack`. Then the board has a current
|
|
378
|
+
* reading of the entry at each instant after its first read.
|
|
379
|
+
*/
|
|
380
|
+
export declare function readInterval(entry: WatchEntry, options: ScheduleOptions): ReadInterval;
|
|
381
|
+
/** This function gives the problems of a watch: an entry with no interval, and two entries with one name. */
|
|
382
|
+
export declare function watchProblems(entries: readonly WatchEntry[], options: ScheduleOptions): string[];
|
|
383
|
+
/** This record is the plan of one pass of a watch: the due entries, and the instant of the next due read. */
|
|
384
|
+
interface ReadPlan {
|
|
385
|
+
/** The names of the due entries, in the order of the entries. An entry with no read is due. */
|
|
386
|
+
readonly due: readonly string[];
|
|
387
|
+
/** The first instant after `now` that an entry is due, or null when no entry has an interval. */
|
|
388
|
+
readonly next: string | null;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* This function plans one pass of a watch. An entry is due when it has no last read, or when its interval has
|
|
392
|
+
* passed since its last read. An entry with no interval is not due at any time. `watchProblems` gives its reason.
|
|
393
|
+
*
|
|
394
|
+
* @param last - The instant of the last read of each entry, by name
|
|
395
|
+
* @throws RangeError when `now` or a last read is not an instant.
|
|
396
|
+
*/
|
|
397
|
+
export declare function planReads(entries: readonly WatchEntry[], last: Readonly<Record<string, string>>, now: string, options: ScheduleOptions): ReadPlan;
|
|
398
|
+
/**
|
|
399
|
+
* This function gives the check digit of the digits of a GTIN without its last digit.
|
|
400
|
+
*
|
|
401
|
+
* @throws RangeError when the text has a character that is not a digit, or no character.
|
|
402
|
+
*/
|
|
403
|
+
export declare function gtinCheckDigit(body: string): number;
|
|
404
|
+
/**
|
|
405
|
+
* This function gives the GTIN-14 form of a GTIN, or null. It removes the spaces and the hyphens first.
|
|
406
|
+
* It refuses a length other than 8, 12, 13 or 14 digits, a wrong check digit, and a GTIN of zeros only.
|
|
407
|
+
*/
|
|
408
|
+
export declare function parseGtin(text: string): string | null;
|
|
409
|
+
/**
|
|
410
|
+
* This function gives the unit price for an order of `count` units. The price of the largest break at or
|
|
411
|
+
* below the count applies, or the price of the reading when no break applies.
|
|
412
|
+
*
|
|
413
|
+
* @throws RangeError when the count is not a whole number, or when it is less than the minimum of the reading.
|
|
414
|
+
*/
|
|
415
|
+
export declare function unitPriceFor(reading: PriceReading, count: number): Quantity;
|
|
416
|
+
/**
|
|
417
|
+
* This function gives the cost of an order of `count` units with its shipping, or null when the shipping
|
|
418
|
+
* cost is not known. The cost is exact, and the function does not round it to the cent.
|
|
419
|
+
*
|
|
420
|
+
* @throws RangeError when the count is not a whole number, or when it is less than the minimum of the reading.
|
|
421
|
+
*/
|
|
422
|
+
export declare function costOf(reading: PriceReading, count: number): Quantity | null;
|
|
423
|
+
export type { Availability, BestOffer, BestOfferOptions, Board, BoardDrop, BoardRead, Condition, Dropped, Format, Identifiers, KeepOptions, Kept, Observation, Offer, OfferChoice, PartNumber, PartWant, PriceBreak, PriceReading, Product, Rating, ReadInterval, ReadPlan, Retention, ScheduleOptions, Seller, SettleOptions, Settled, Shipping, SourceTerms, Spec, WatchEntry };
|
|
424
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/identifiers.ts","../src/records.ts","../src/offers.ts","../src/terms.ts","../src/board.ts","../src/gtin.ts","../src/price.ts"],"mappings":";;;;;;;;;UAGiB;WACN;WACA;;;UAIM;WACN;WACA,sBAAsB;;;;;;wBAOjB,SA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