@defra/lis-species-cattle 0.10.8 → 0.11.0-lreg-896.alpha.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/README.md CHANGED
@@ -76,6 +76,43 @@ getBreedCode('aberdeen angus') // 'AA'
76
76
  getBreedCode("Blonde D'Aquitaine") // 'BA'
77
77
  ```
78
78
 
79
+ ### `earTagFormat(earTagNumber)`
80
+
81
+ Returns which shape an official cattle ear tag number is, or `undefined` when it is not one.
82
+ The number is read as typed: any case, and any run of spaces as one.
83
+
84
+ | Shape | Constant | Example | Cattle |
85
+ | ---------------- | ----------------- | ---------------- | ------------------------------------------------------ |
86
+ | `ukNumeric` | `UK_NUMERIC` | `UK123456789012` | Born in Great Britain since July 2000 |
87
+ | `xiNumeric` | `XI_NUMERIC` | `XI012345678901` | Born in Northern Ireland since June 2025 |
88
+ | `ukAlphanumeric` | `UK_ALPHANUMERIC` | `UKAB1234 00001` | Born April 1995 to 1999, and some from the Isle of Man |
89
+ | `ukCheckLetter` | `UK_CHECK_LETTER` | `UK123456 78A` | Older Northern Ireland tags |
90
+ | `countryNumeric` | `COUNTRY_NUMERIC` | `FR12345 67890` | Imported, and Northern Ireland's shorter numeric tags |
91
+ | `noCountryCode` | `NO_COUNTRY_CODE` | `Z4321 321` | Born before April 1995 |
92
+
93
+ `ANY_EAR_TAG_FORMAT` lists every shape. The check digit of a `ukNumeric` number is not checked.
94
+
95
+ ```js
96
+ import { UK_NUMERIC, earTagFormat } from '@defra/lis-species-cattle'
97
+
98
+ earTagFormat('uk 230011 200123') === UK_NUMERIC // true
99
+ earTagFormat('Bob the bull') // undefined
100
+ ```
101
+
102
+ ### `isSameEarTag(one, other)`
103
+
104
+ Returns `true` when two ear tag numbers are the same number, however each is cased. Spaces are
105
+ ignored in the `ukNumeric`, `xiNumeric` and `ukAlphanumeric` shapes, where they only group the
106
+ characters. In the other shapes a space divides the herd mark from the animal number, so numbers
107
+ that differ in where it falls are different animals.
108
+
109
+ ```js
110
+ import { isSameEarTag } from '@defra/lis-species-cattle'
111
+
112
+ isSameEarTag('UK123456789012', 'uk 123456 789012') // true
113
+ isSameEarTag('UK91234 56789', 'UK912345 6789') // false
114
+ ```
115
+
79
116
  ## Prerequisites
80
117
 
81
118
  - Node.js 24 (see `.nvmrc`, use `nvm use`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-species-cattle",
3
- "version": "0.10.8",
3
+ "version": "0.11.0-lreg-896.alpha.2",
4
4
  "description": "Cattle shared package for livestock taxonomy flows.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -0,0 +1,128 @@
1
+ /*
2
+ * The shapes an official cattle ear tag number takes, and which of them a
3
+ * number is. They come from the formats the Cattle Tracing System (CTS)
4
+ * holds, and from the tags of cattle imported to Great Britain, which keep
5
+ * the number of the country they were born in.
6
+ *
7
+ * A number is read as typed, in any case, with any run of spaces as one. In
8
+ * the numeric UK and XI shapes spaces only group the digits, so they are
9
+ * ignored. In every other shape a space divides the herd mark from the
10
+ * animal number, and two animals can differ only in where it falls, so it
11
+ * is kept.
12
+ */
13
+
14
+ /** UK and twelve digits: every calf born in Great Britain since July 2000. */
15
+ export const UK_NUMERIC = 'ukNumeric'
16
+ /** XI and twelve digits: calves born in Northern Ireland since June 2025. */
17
+ export const XI_NUMERIC = 'xiNumeric'
18
+ /**
19
+ * UK, a herd mark of letters then digits, and an animal number: cattle born
20
+ * from April 1995 to 1999 (Barimo, to CTS), and some from the Isle of Man.
21
+ */
22
+ export const UK_ALPHANUMERIC = 'ukAlphanumeric'
23
+ /** UK, digits and a closing check letter: older Northern Ireland tags. */
24
+ export const UK_CHECK_LETTER = 'ukCheckLetter'
25
+ /**
26
+ * A country code and up to twelve digits, as European law has it: imported
27
+ * cattle, and Northern Ireland's numeric tags of fewer than twelve digits.
28
+ * Ireland's tags carry 372 for IE.
29
+ */
30
+ export const COUNTRY_NUMERIC = 'countryNumeric'
31
+ /**
32
+ * A herd mark and an animal number with no country code: cattle born before
33
+ * April 1995 (Pre-Barimo, to CTS).
34
+ */
35
+ export const NO_COUNTRY_CODE = 'noCountryCode'
36
+
37
+ /** Every shape, for an animal that may be of any age or from anywhere. */
38
+ export const ANY_EAR_TAG_FORMAT = Object.freeze([
39
+ UK_NUMERIC,
40
+ XI_NUMERIC,
41
+ UK_ALPHANUMERIC,
42
+ UK_CHECK_LETTER,
43
+ COUNTRY_NUMERIC,
44
+ NO_COUNTRY_CODE
45
+ ])
46
+
47
+ const MIN_COUNTRY_DIGITS = 6
48
+ const MAX_COUNTRY_DIGITS = 12
49
+
50
+ const UK_NUMERIC_PATTERN = /^UK\d{12}$/
51
+ const XI_NUMERIC_PATTERN = /^XI\d{12}$/
52
+ const UK_ALPHANUMERIC_PATTERN = /^UK ?[A-Z]{1,2}\d{3,4} ?\d{5}$/
53
+ const UK_CHECK_LETTER_PATTERN = /^UK ?\d{3,6} \d{1,4}[A-Z]$/
54
+ const COUNTRY_NUMERIC_PATTERN = /^(?:[A-Z]{2}|372)[\d ]+$/
55
+ const NO_COUNTRY_CODE_PATTERN = /^[A-Z]{1,2}\d{3,4} \d{1,5}[A-Z]?$/
56
+
57
+ /**
58
+ * @param {string} spaced - a number with single spaces
59
+ * @returns {boolean} true for a country code followed by digits, of a count
60
+ * a tag can have
61
+ */
62
+ function isCountryNumeric(spaced) {
63
+ if (!COUNTRY_NUMERIC_PATTERN.test(spaced)) {
64
+ return false
65
+ }
66
+ const digits = spaced.replace(/^372/, '').replaceAll(/\D/g, '').length
67
+ return digits >= MIN_COUNTRY_DIGITS && digits <= MAX_COUNTRY_DIGITS
68
+ }
69
+
70
+ /**
71
+ * Which shape an ear tag number is. The numeric UK and XI shapes are looked
72
+ * for first, as the most exact. Two letters then digits is read as a
73
+ * country code, so it is a country's number before it is one with no
74
+ * country code.
75
+ * @param {string} earTagNumber - as typed
76
+ * @returns {string | undefined} the shape, or undefined when it is not an
77
+ * ear tag number
78
+ */
79
+ export function earTagFormat(earTagNumber) {
80
+ const spaced = earTagNumber.trim().toUpperCase().replaceAll(/\s+/g, ' ')
81
+ const unspaced = spaced.replaceAll(' ', '')
82
+
83
+ if (UK_NUMERIC_PATTERN.test(unspaced)) {
84
+ return UK_NUMERIC
85
+ }
86
+ if (XI_NUMERIC_PATTERN.test(unspaced)) {
87
+ return XI_NUMERIC
88
+ }
89
+ if (UK_ALPHANUMERIC_PATTERN.test(spaced)) {
90
+ return UK_ALPHANUMERIC
91
+ }
92
+ if (UK_CHECK_LETTER_PATTERN.test(spaced)) {
93
+ return UK_CHECK_LETTER
94
+ }
95
+ if (isCountryNumeric(spaced)) {
96
+ return COUNTRY_NUMERIC
97
+ }
98
+ if (NO_COUNTRY_CODE_PATTERN.test(spaced)) {
99
+ return NO_COUNTRY_CODE
100
+ }
101
+ return undefined
102
+ }
103
+
104
+ /** The shapes in which a space only groups the characters. */
105
+ const SPACES_IGNORED = new Set([UK_NUMERIC, XI_NUMERIC, UK_ALPHANUMERIC])
106
+
107
+ /**
108
+ * @param {string} earTagNumber - as typed
109
+ * @returns {string} the number in one case and spacing, to compare it by
110
+ */
111
+ function comparable(earTagNumber) {
112
+ const spaced = earTagNumber.trim().toUpperCase().replaceAll(/\s+/g, ' ')
113
+ return SPACES_IGNORED.has(earTagFormat(spaced) ?? '')
114
+ ? spaced.replaceAll(' ', '')
115
+ : spaced
116
+ }
117
+
118
+ /**
119
+ * @param {string} one - an ear tag number as typed
120
+ * @param {string} other - another
121
+ * @returns {boolean} true when they are the same number, however each was
122
+ * cased, and however each was spaced where spaces only group the
123
+ * characters. Where a space divides the herd mark from the animal number,
124
+ * numbers that differ in where it falls are different animals.
125
+ */
126
+ export function isSameEarTag(one, other) {
127
+ return comparable(one) === comparable(other)
128
+ }
package/src/index.js CHANGED
@@ -1,4 +1,15 @@
1
1
  export { breeds, getBreedCode, getBreedName } from './breeds.js'
2
+ export {
3
+ ANY_EAR_TAG_FORMAT,
4
+ COUNTRY_NUMERIC,
5
+ NO_COUNTRY_CODE,
6
+ UK_ALPHANUMERIC,
7
+ UK_CHECK_LETTER,
8
+ UK_NUMERIC,
9
+ XI_NUMERIC,
10
+ earTagFormat,
11
+ isSameEarTag
12
+ } from './ear-tags.js'
2
13
 
3
14
  export const species = {
4
15
  id: 'cattle',