rut.ts 2.0.0 → 3.0.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 @hansfpc.
3
+ Copyright (c) 2026 Arrow Software.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -3,17 +3,42 @@
3
3
  <h1>Rut.ts: Handle chilean RUT values with ease using TypeScript.</h1>
4
4
  </div>
5
5
 
6
+ ## What is a RUT?
7
+
8
+ The **RUT** (Rol Único Tributario) is the unique Chilean identification number used for:
9
+ - Tax purposes
10
+ - Legal identification
11
+ - Government services
12
+ - Banking and financial transactions
13
+
14
+ **Format**: `XX.XXX.XXX-Y` where:
15
+ - `X` = Body (7-8 digits)
16
+ - `Y` = Verifier digit (0-9 or K)
17
+
18
+ **Example**: `12.345.678-5`
19
+
20
+ The verifier digit is calculated using the [Modulo 11 algorithm](https://en.wikipedia.org/wiki/Rol_%C3%9Anico_Tributario) to validate the RUT's authenticity.
21
+
22
+ ---
23
+
6
24
  ## Features
7
25
 
8
26
  - **Validation**: Check if a RUT is valid, with optional strict mode to detect suspicious patterns.
9
27
  - **Cleaning**: Remove extraneous characters and leading zeros from RUT strings.
10
28
  - **Formatting**: Convert RUTs into a standardized format, with or without dots.
29
+ - **Incremental Formatting**: Format RUTs progressively as the user types (ideal for form inputs).
11
30
  - **Decomposition**: Split a RUT into its body and verifier digit.
12
31
  - **Generation**: Generate valid random RUT numbers.
13
32
  - **Calculate Verifier**: Calculate the verifier digit for a given RUT body.
33
+ - **Format Detection**: Check if a string looks like a RUT format with `isRutLike`.
34
+ - **Safe Mode**: All functions support `throwOnError: false` to return `null` instead of throwing errors.
14
35
 
15
36
  ## Installation
16
37
 
38
+ Using [bun](https://bun.sh/):
39
+
40
+ $ bun add rut.ts
41
+
17
42
  Using [npm](https://www.npmjs.com/):
18
43
 
19
44
  $ npm install rut.ts
@@ -23,9 +48,86 @@ Using [pnpm](https://pnpm.io/):
23
48
  $ pnpm install rut.ts
24
49
 
25
50
 
51
+ ## Quick Examples
52
+
53
+ ```typescript
54
+ import { validate, format, clean, isRutLike, decompose } from 'rut.ts'
55
+
56
+ // Validation
57
+ validate('12.345.678-5') // true
58
+ validate('12.345.678-0') // false (wrong verifier)
59
+ validate('11.111.111-1', { strict: true }) // false (strict mode rejects suspicious RUTs)
60
+
61
+ // Formatting
62
+ format('123456785') // '12.345.678-5'
63
+ format('123456785', { dots: false }) // '12345678-5'
64
+
65
+ // Incremental formatting (for form inputs)
66
+ format('1234', { incremental: true }) // '1.234'
67
+ format('12345678', { incremental: true }) // '1.234.567-8' (8 chars = complete)
68
+ format('123456785', { incremental: true }) // '12.345.678-5' (9 chars = complete)
69
+
70
+ // Cleaning
71
+ clean('12.345.678-5') // '123456785'
72
+
73
+ // Decomposition
74
+ const { body, verifier } = decompose('12.345.678-5')
75
+ console.log(body) // '12345678'
76
+ console.log(verifier) // '5'
77
+
78
+ // Format detection (without full validation)
79
+ isRutLike('12.345.678-5') // true
80
+ isRutLike('not-a-rut') // false
81
+
82
+ // Safe mode (returns null instead of throwing)
83
+ clean('invalid', { throwOnError: false }) // null
84
+ format('abc', { throwOnError: false }) // null
85
+ ```
86
+
87
+ ## Incremental Formatting
88
+
89
+ The `incremental` option in `format()` allows you to format RUTs progressively as the user types. This is useful for real-time formatting in form inputs.
90
+
91
+ ```typescript
92
+ // Example: React input handler
93
+ const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
94
+ const formatted = format(e.target.value, { incremental: true })
95
+ setRut(formatted)
96
+ }
97
+ ```
98
+
99
+ > **Note**: Incremental formatting will format the input progressively even for incomplete RUTs. The formatted output may not represent a valid RUT until the input is complete. Always use `validate()` to verify the final RUT before processing.
100
+
101
+ ### When to use incremental mode
102
+
103
+ **✅ Use incremental when:**
104
+ - Formatting user input in real-time as they type
105
+ - Providing immediate visual feedback in form fields
106
+ - Improving UX with progressive formatting
107
+
108
+ **❌ Don't use incremental when:**
109
+ - Formatting already complete/stored RUTs (use default `format()`)
110
+ - Validating RUTs (use `validate()` instead)
111
+ - Processing final form submission values
112
+
26
113
  ## Usage
27
114
 
28
- Please refer to [the documentation](https://rutts-arrowsw.vercel.app/) for examples on how to use this library.
115
+ Please refer to [the documentation](https://rutts-arrowsw.vercel.app/) for more detailed examples.
116
+
117
+
118
+ ## TypeScript Types
119
+
120
+ The library exports the following types:
121
+
122
+ ```typescript
123
+ import type { DecomposedRut, FormatOptions, SafeOptions, ValidateOptions, VerifierDigit } from 'rut.ts'
124
+
125
+ // VerifierDigit: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'K'
126
+ // DecomposedRut: { body: string; verifier: string }
127
+ // FormatOptions: { incremental?: boolean; dots?: boolean; throwOnError?: boolean }
128
+ // ValidateOptions: { strict?: boolean }
129
+ // SafeOptions: { throwOnError?: boolean }
130
+ ```
29
131
 
30
132
 
31
133
  ## Contributing
@@ -2,61 +2,144 @@ type DecomposedRut = {
2
2
  body: string;
3
3
  verifier: string;
4
4
  };
5
+ type FormatOptions = {
6
+ incremental?: boolean;
7
+ dots?: boolean;
8
+ throwOnError?: boolean;
9
+ };
10
+ type SafeOptions = {
11
+ throwOnError?: boolean;
12
+ };
13
+ type ValidateOptions = {
14
+ strict?: boolean;
15
+ };
16
+ type VerifierDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'K';
5
17
  export declare const getInvalidRutError: (rut: string) => string;
6
18
  /**
7
- * Cleans the input string by removing leading zeros, non-numeric characters, and ensures the RUT is uppercased. Throws an error if the cleaned RUT is not within the valid length range or if 'K' is not in the correct position.
19
+ * Cleans the input string by removing leading zeros, non-numeric characters, and ensures the RUT is uppercased.
8
20
  * @param {string} rut - The RUT string to clean.
9
- * @returns {string} The cleaned RUT string.
10
- * @throws {Error} If the RUT is not valid.
21
+ * @param {SafeOptions} [options] - Configuration options.
22
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null.
23
+ * @returns {string | null} The cleaned RUT string, or null if invalid and throwOnError is false.
24
+ * @throws {Error} If the RUT is not valid and throwOnError is true.
11
25
  */
12
- declare const clean: (rut: string) => string;
26
+ declare function clean(rut: string): string;
27
+ declare function clean(rut: string, options: {
28
+ throwOnError: false;
29
+ }): string | null;
30
+ declare function clean(rut: string, options: {
31
+ throwOnError: true;
32
+ }): string;
33
+ declare function clean(rut: string, options?: SafeOptions): string | null;
13
34
  /**
14
35
  * Extracts the body (part before the verifier digit) from a given RUT string.
15
36
  * This function first cleans the input RUT string to ensure it is in a valid format before slicing.
16
37
  * @param {string} rut - The RUT string from which to extract the body.
17
- * @returns {string} The body of the RUT.
18
- * @throws {Error} If the cleaned RUT is not valid.
38
+ * @param {SafeOptions} [options] - Configuration options.
39
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null.
40
+ * @returns {string | null} The body of the RUT, or null if invalid and throwOnError is false.
41
+ * @throws {Error} If the cleaned RUT is not valid and throwOnError is true.
19
42
  */
20
- declare const getBody: (rut: string) => string;
43
+ declare function getBody(rut: string): string;
44
+ declare function getBody(rut: string, options: {
45
+ throwOnError: false;
46
+ }): string | null;
47
+ declare function getBody(rut: string, options: {
48
+ throwOnError: true;
49
+ }): string;
50
+ declare function getBody(rut: string, options?: SafeOptions): string | null;
21
51
  /**
22
52
  * Extracts the verifier digit (the last character) from a given RUT string.
23
53
  * This function cleans the input RUT string to ensure it is in a valid format before extracting the verifier digit.
24
54
  * @param {string} rut - The RUT string from which to extract the verifier digit.
25
- * @returns {string} The verifier digit of the RUT.
26
- * @throws {Error} If the cleaned RUT is not valid.
55
+ * @param {SafeOptions} [options] - Configuration options.
56
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null.
57
+ * @returns {string | null} The verifier digit of the RUT, or null if invalid and throwOnError is false.
58
+ * @throws {Error} If the cleaned RUT is not valid and throwOnError is true.
27
59
  */
28
- declare const getVerifier: (rut: string) => string;
60
+ declare function getVerifier(rut: string): VerifierDigit;
61
+ declare function getVerifier(rut: string, options: {
62
+ throwOnError: false;
63
+ }): VerifierDigit | null;
64
+ declare function getVerifier(rut: string, options: {
65
+ throwOnError: true;
66
+ }): VerifierDigit;
67
+ declare function getVerifier(rut: string, options?: SafeOptions): VerifierDigit | null;
29
68
  /**
30
69
  * Decomposes the given RUT into its body and verifier parts.
31
70
  * @param {string} rut - The RUT string to decompose.
32
- * @returns {DecomposedRut} An object containing the body and verifier of the RUT.
71
+ * @param {SafeOptions} [options] - Configuration options.
72
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null.
73
+ * @returns {DecomposedRut | null} An object containing the body and verifier of the RUT, or null if invalid and throwOnError is false.
74
+ * @throws {Error} If the RUT is not valid and throwOnError is true.
75
+ */
76
+ declare function decompose(rut: string): DecomposedRut;
77
+ declare function decompose(rut: string, options: {
78
+ throwOnError: false;
79
+ }): DecomposedRut | null;
80
+ declare function decompose(rut: string, options: {
81
+ throwOnError: true;
82
+ }): DecomposedRut;
83
+ declare function decompose(rut: string, options?: SafeOptions): DecomposedRut | null;
84
+ /**
85
+ * Checks if a string has a valid RUT format (without validating the verifier digit).
86
+ * Useful for quick format validation in UI before full validation.
87
+ * @param {string} rut - The string to check.
88
+ * @returns {boolean} True if the string looks like a RUT format, false otherwise.
33
89
  */
34
- declare const decompose: (rut: string) => DecomposedRut;
90
+ declare const isRutLike: (rut: string) => boolean;
35
91
  /**
36
92
  * Calculates the verifier digit for a given RUT body.
37
93
  * @param {string} rutBody - The body of the RUT for which to calculate the verifier.
38
- * @returns {string} The calculated verifier digit.
39
- * @throws {Error} If the RUT body, after being cleaned, is empty.
94
+ * @param {SafeOptions} [options] - Configuration options.
95
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null.
96
+ * @returns {string | null} The calculated verifier digit, or null if invalid and throwOnError is false.
97
+ * @throws {Error} If the RUT body is invalid and throwOnError is true.
40
98
  */
41
- declare const calculateVerifier: (rutBody: string) => string;
99
+ declare function calculateVerifier(rutBody: string): VerifierDigit;
100
+ declare function calculateVerifier(rutBody: string, options: {
101
+ throwOnError: false;
102
+ }): VerifierDigit | null;
103
+ declare function calculateVerifier(rutBody: string, options: {
104
+ throwOnError: true;
105
+ }): VerifierDigit;
106
+ declare function calculateVerifier(rutBody: string, options?: SafeOptions): VerifierDigit | null;
42
107
  /**
43
108
  * Validates a given RUT string, optionally with strict mode to also check for suspicious patterns.
44
- * @param {string} rut - The RUT string to validate.
45
- * @param {boolean} [strict=false] - Whether to use strict mode for validation.
109
+ * @param {unknown} rut - The RUT string to validate.
110
+ * @param {ValidateOptions} [options] - Validation options.
111
+ * @param {boolean} [options.strict=false] - If true, rejects suspicious RUTs (e.g., 11.111.111-1).
46
112
  * @returns {boolean} True if the RUT is valid, false otherwise.
47
113
  */
48
- declare const validate: (rut: string, strict?: boolean) => boolean;
114
+ declare const validate: (rut: unknown, options?: ValidateOptions) => boolean;
49
115
  /**
50
116
  * Formats a given RUT string, with options for incremental formatting and dot separators.
117
+ *
118
+ * **Note on incremental mode**: When `incremental: true`, the function formats the RUT progressively
119
+ * as the user types, even for incomplete RUTs. This is useful for real-time formatting in form inputs,
120
+ * but the formatted output may not represent a valid RUT until the input is complete.
121
+ * Always use `validate()` to verify the final RUT.
122
+ *
51
123
  * @param {string} rut - The RUT string to format.
52
- * @param {boolean} [incremental=false] - Whether to format the RUT incrementally.
53
- * @param {boolean} [dots=true] - Whether to include dot separators in the formatted RUT.
54
- * @returns {string} The formatted RUT string.
124
+ * @param {FormatOptions} [options] - The formatting options.
125
+ * @param {boolean} [options.incremental=false] - Whether to format the RUT incrementally (for real-time input formatting).
126
+ * @param {boolean} [options.dots=true] - Whether to include dot separators in the formatted RUT.
127
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null. Ignored in incremental mode.
128
+ * @returns {string | null} The formatted RUT string, or null if invalid and throwOnError is false.
129
+ * @throws {Error} If the RUT is invalid and throwOnError is true (only in non-incremental mode).
55
130
  */
56
- declare const format: (rut: string, incremental?: boolean, dots?: boolean) => string;
131
+ declare function format(rut: string, options?: Omit<FormatOptions, 'throwOnError'>): string;
132
+ declare function format(rut: string, options: FormatOptions & {
133
+ throwOnError: false;
134
+ }): string | null;
135
+ declare function format(rut: string, options: FormatOptions & {
136
+ throwOnError: true;
137
+ }): string;
138
+ declare function format(rut: string, options?: FormatOptions): string | null;
57
139
  /**
58
140
  * Generates a random valid RUT string.
59
141
  * @returns {string} A randomly generated, valid RUT string.
60
142
  */
61
143
  declare const generate: () => string;
62
- export { validate, clean, format, calculateVerifier, getBody, getVerifier, decompose, generate };
144
+ export { validate, clean, format, calculateVerifier, getBody, getVerifier, decompose, generate, isRutLike };
145
+ export type { DecomposedRut, FormatOptions, SafeOptions, ValidateOptions, VerifierDigit };
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAGO,MAAM,kBAAkB,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC,WAAW,GAAG,+BAA+B,CAAA;AAA3F,QAAA,kBAAkB,sBAAyE;AAExG,MAAM,cAAc,GAAG,CAAC,CAAA;AACxB,MAAM,cAAc,GAAG,CAAC,CAAA;AAExB,MAAM,QAAQ,GAAuB;IACnC,QAAQ,EAAE,gBAAgB;IAC1B,OAAO,EAAE,oCAAoC;IAC7C,UAAU,EAAE,wCAAwC;CACrD,CAAA;AAED;;;;;GAKG;AACH,MAAM,KAAK,GAAG,CAAC,GAAW,EAAU,EAAE;IACpC,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,CAAA;IACjE,IAAI,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,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;AAC9E,CAAC;AAYyB,wBAAM;AAVhC;;;GAGG;AACH,MAAM,QAAQ,GAAG,GAAW,EAAE;IAC5B,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAA;IACvE,MAAM,QAAQ,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAA;IACxC,OAAO,MAAM,CAAC,IAAI,GAAG,QAAQ,CAAC,CAAA;AAChC,CAAC,CAAA;AAEqF,4BAAQ"}
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.generate=exports.decompose=exports.getVerifier=exports.getBody=exports.calculateVerifier=exports.format=exports.clean=exports.validate=exports.getInvalidRutError=void 0;const getInvalidRutError=e=>`String "${e}" is not valid as a RUT input`;exports.getInvalidRutError=getInvalidRutError;const MIN_RUT_LENGTH=8,MAX_RUT_LENGTH=9,patterns={cleaning:/^0+|[^0-9kK]+/g,rutLike:/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/,suspicious:/^(\d)\1?\.?(\1{3})\.?(\1{3})-?(\d|k)?$/},clean=e=>{const t=e.replace(patterns.cleaning,"").toUpperCase();if(t.length<8||t.length>9)throw new Error((0,exports.getInvalidRutError)(e));if(t.includes("K")&&t.indexOf("K")!==t.length-1)throw new Error((0,exports.getInvalidRutError)(e));return t};exports.clean=clean;const getBody=e=>clean(e).slice(0,-1);exports.getBody=getBody;const getVerifier=e=>clean(e).slice(-1);exports.getVerifier=getVerifier;const decompose=e=>({body:getBody(e),verifier:getVerifier(e)});exports.decompose=decompose;const isRutLike=e=>patterns.rutLike.test(e),isSuspicious=e=>patterns.suspicious.test(e),calculateVerifier=e=>{const t=clean(e);if(0===t.length)throw new Error((0,exports.getInvalidRutError)(e));const r=11-t.split("").reverse().reduce(((e,t,r)=>e+Number(t)*(r%6+2)),0)%11;return 11===r?"0":10===r?"K":r.toString()};exports.calculateVerifier=calculateVerifier;const validate=(e,t)=>{if(!isRutLike(e))return!1;if(t&&isSuspicious(e))return!1;const{body:r,verifier:o}=decompose(e);return calculateVerifier(r)===o};exports.validate=validate;const format=(e,t=!1,r=!0)=>{if(0===e.length)return"";if(e.length<=6&&!r)return e;const o=clean(e);if(t){let e=o.slice(-1);o.length>1&&(e=o.slice(-4,-1)+"-"+e);for(let t=4;t<o.length;t+=3){const i=o.length-3-t<0?0:o.length-3-t;e=r?o.slice(i,o.length-t)+"."+e:o.slice(i,o.length-t)+e}return e}if(r){let e=o.slice(-4,-1)+"-"+o.substring(o.length-1);for(let t=4;t<o.length;t+=3)e=o.slice(-3-t,-t)+"."+e;return e}return o.slice(0,-1)+"-"+o.substring(o.length-1)};exports.format=format;const generate=()=>{const e=Math.floor(10000003+9e7*Math.random()).toString(),t=calculateVerifier(e);return format(e+t)};exports.generate=generate;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.isRutLike=exports.generate=exports.decompose=exports.getVerifier=exports.getBody=exports.calculateVerifier=exports.format=exports.clean=exports.validate=exports.getInvalidRutError=void 0;const getInvalidRutError=r=>`String "${r}" is not valid as a RUT input`;exports.getInvalidRutError=getInvalidRutError;const withThrowOption=r=>({throwOnError:null==r||r}),MIN_RUT_LENGTH=8,MAX_RUT_LENGTH=9,cleanRaw=r=>r.replace(/^0+|[^0-9kK]+/g,"").toUpperCase(),patterns={cleaning:/^0+|[^0-9kK]+/g,rutLike:/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/,suspicious:/^(\d)\1?\.?(\1{3})\.?(\1{3})-?(\d|k)?$/};function clean(r,t){var e;const n=null===(e=null==t?void 0:t.throwOnError)||void 0===e||e,o=r.replace(patterns.cleaning,"").toUpperCase();if(o.length<MIN_RUT_LENGTH||o.length>MAX_RUT_LENGTH){if(n)throw new Error((0,exports.getInvalidRutError)(r));return null}if(o.includes("K")&&o.indexOf("K")!==o.length-1){if(n)throw new Error((0,exports.getInvalidRutError)(r));return null}return o}function getBody(r,t){var e;const n=clean(r,withThrowOption(null==t?void 0:t.throwOnError));return null!==(e=null==n?void 0:n.slice(0,-1))&&void 0!==e?e:null}function getVerifier(r,t){var e;const n=clean(r,withThrowOption(null==t?void 0:t.throwOnError));return null!==(e=null==n?void 0:n.slice(-1))&&void 0!==e?e:null}function decompose(r,t){const e=withThrowOption(null==t?void 0:t.throwOnError),n=getBody(r,e),o=getVerifier(r,e);return null===n||null===o?null:{body:n,verifier:o}}exports.clean=clean,exports.getBody=getBody,exports.getVerifier=getVerifier,exports.decompose=decompose;const isRutLike=r=>patterns.rutLike.test(r);exports.isRutLike=isRutLike;const isSuspicious=r=>patterns.suspicious.test(r);function calculateVerifier(r,t){const e=withThrowOption(null==t?void 0:t.throwOnError),n=cleanRaw(r);if(n.length<7||n.length>8){if(e.throwOnError)throw new Error((0,exports.getInvalidRutError)(r));return null}if(!/^\d+$/.test(n)){if(e.throwOnError)throw new Error((0,exports.getInvalidRutError)(r));return null}const o=11-n.split("").reverse().reduce(((r,t,e)=>r+Number(t)*(e%6+2)),0)%11;return 11===o?"0":10===o?"K":o.toString()}exports.calculateVerifier=calculateVerifier;const validate=(r,t)=>{if("string"!=typeof r||0===r.length)return!1;if(!isRutLike(r))return!1;if((null==t?void 0:t.strict)&&isSuspicious(r))return!1;const e=decompose(r,{throwOnError:!1});if(!e)return!1;const n=calculateVerifier(e.body,{throwOnError:!1});return!!n&&n===e.verifier};function format(r,t){var e,n,o;const i={incremental:null!==(e=null==t?void 0:t.incremental)&&void 0!==e&&e,dots:null===(n=null==t?void 0:t.dots)||void 0===n||n,throwOnError:null===(o=null==t?void 0:t.throwOnError)||void 0===o||o};if(0===r.length)return"";if(i.incremental){const t=cleanRaw(r);if(0===t.length)return"";if(t.length<MIN_RUT_LENGTH){if(!i.dots||t.length<=3)return t;let r=t.slice(-3);for(let e=3;e<t.length;e+=3){const n=t.length-3-e<0?0:t.length-3-e;r=t.slice(n,t.length-e)+"."+r}return r}let e=t.slice(-1);e=t.slice(-4,-1)+"-"+e;for(let r=4;r<t.length;r+=3){const n=t.length-3-r<0?0:t.length-3-r;e=i.dots?t.slice(n,t.length-r)+"."+e:t.slice(n,t.length-r)+e}return e}const l=clean(r,withThrowOption(i.throwOnError));if(null===l)return null;if(i.dots){let r=l.slice(-4,-1)+"-"+l.substring(l.length-1);for(let t=4;t<l.length;t+=3)r=l.slice(-3-t,-t)+"."+r;return r}return l.slice(0,-1)+"-"+l.substring(l.length-1)}exports.validate=validate,exports.format=format;const generate=()=>{const r=Math.floor(10000003+9e7*Math.random()).toString();return format(r+calculateVerifier(r))};exports.generate=generate;
@@ -2,61 +2,144 @@ type DecomposedRut = {
2
2
  body: string;
3
3
  verifier: string;
4
4
  };
5
+ type FormatOptions = {
6
+ incremental?: boolean;
7
+ dots?: boolean;
8
+ throwOnError?: boolean;
9
+ };
10
+ type SafeOptions = {
11
+ throwOnError?: boolean;
12
+ };
13
+ type ValidateOptions = {
14
+ strict?: boolean;
15
+ };
16
+ type VerifierDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'K';
5
17
  export declare const getInvalidRutError: (rut: string) => string;
6
18
  /**
7
- * Cleans the input string by removing leading zeros, non-numeric characters, and ensures the RUT is uppercased. Throws an error if the cleaned RUT is not within the valid length range or if 'K' is not in the correct position.
19
+ * Cleans the input string by removing leading zeros, non-numeric characters, and ensures the RUT is uppercased.
8
20
  * @param {string} rut - The RUT string to clean.
9
- * @returns {string} The cleaned RUT string.
10
- * @throws {Error} If the RUT is not valid.
21
+ * @param {SafeOptions} [options] - Configuration options.
22
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null.
23
+ * @returns {string | null} The cleaned RUT string, or null if invalid and throwOnError is false.
24
+ * @throws {Error} If the RUT is not valid and throwOnError is true.
11
25
  */
12
- declare const clean: (rut: string) => string;
26
+ declare function clean(rut: string): string;
27
+ declare function clean(rut: string, options: {
28
+ throwOnError: false;
29
+ }): string | null;
30
+ declare function clean(rut: string, options: {
31
+ throwOnError: true;
32
+ }): string;
33
+ declare function clean(rut: string, options?: SafeOptions): string | null;
13
34
  /**
14
35
  * Extracts the body (part before the verifier digit) from a given RUT string.
15
36
  * This function first cleans the input RUT string to ensure it is in a valid format before slicing.
16
37
  * @param {string} rut - The RUT string from which to extract the body.
17
- * @returns {string} The body of the RUT.
18
- * @throws {Error} If the cleaned RUT is not valid.
38
+ * @param {SafeOptions} [options] - Configuration options.
39
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null.
40
+ * @returns {string | null} The body of the RUT, or null if invalid and throwOnError is false.
41
+ * @throws {Error} If the cleaned RUT is not valid and throwOnError is true.
19
42
  */
20
- declare const getBody: (rut: string) => string;
43
+ declare function getBody(rut: string): string;
44
+ declare function getBody(rut: string, options: {
45
+ throwOnError: false;
46
+ }): string | null;
47
+ declare function getBody(rut: string, options: {
48
+ throwOnError: true;
49
+ }): string;
50
+ declare function getBody(rut: string, options?: SafeOptions): string | null;
21
51
  /**
22
52
  * Extracts the verifier digit (the last character) from a given RUT string.
23
53
  * This function cleans the input RUT string to ensure it is in a valid format before extracting the verifier digit.
24
54
  * @param {string} rut - The RUT string from which to extract the verifier digit.
25
- * @returns {string} The verifier digit of the RUT.
26
- * @throws {Error} If the cleaned RUT is not valid.
55
+ * @param {SafeOptions} [options] - Configuration options.
56
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null.
57
+ * @returns {string | null} The verifier digit of the RUT, or null if invalid and throwOnError is false.
58
+ * @throws {Error} If the cleaned RUT is not valid and throwOnError is true.
27
59
  */
28
- declare const getVerifier: (rut: string) => string;
60
+ declare function getVerifier(rut: string): VerifierDigit;
61
+ declare function getVerifier(rut: string, options: {
62
+ throwOnError: false;
63
+ }): VerifierDigit | null;
64
+ declare function getVerifier(rut: string, options: {
65
+ throwOnError: true;
66
+ }): VerifierDigit;
67
+ declare function getVerifier(rut: string, options?: SafeOptions): VerifierDigit | null;
29
68
  /**
30
69
  * Decomposes the given RUT into its body and verifier parts.
31
70
  * @param {string} rut - The RUT string to decompose.
32
- * @returns {DecomposedRut} An object containing the body and verifier of the RUT.
71
+ * @param {SafeOptions} [options] - Configuration options.
72
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null.
73
+ * @returns {DecomposedRut | null} An object containing the body and verifier of the RUT, or null if invalid and throwOnError is false.
74
+ * @throws {Error} If the RUT is not valid and throwOnError is true.
75
+ */
76
+ declare function decompose(rut: string): DecomposedRut;
77
+ declare function decompose(rut: string, options: {
78
+ throwOnError: false;
79
+ }): DecomposedRut | null;
80
+ declare function decompose(rut: string, options: {
81
+ throwOnError: true;
82
+ }): DecomposedRut;
83
+ declare function decompose(rut: string, options?: SafeOptions): DecomposedRut | null;
84
+ /**
85
+ * Checks if a string has a valid RUT format (without validating the verifier digit).
86
+ * Useful for quick format validation in UI before full validation.
87
+ * @param {string} rut - The string to check.
88
+ * @returns {boolean} True if the string looks like a RUT format, false otherwise.
33
89
  */
34
- declare const decompose: (rut: string) => DecomposedRut;
90
+ declare const isRutLike: (rut: string) => boolean;
35
91
  /**
36
92
  * Calculates the verifier digit for a given RUT body.
37
93
  * @param {string} rutBody - The body of the RUT for which to calculate the verifier.
38
- * @returns {string} The calculated verifier digit.
39
- * @throws {Error} If the RUT body, after being cleaned, is empty.
94
+ * @param {SafeOptions} [options] - Configuration options.
95
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null.
96
+ * @returns {string | null} The calculated verifier digit, or null if invalid and throwOnError is false.
97
+ * @throws {Error} If the RUT body is invalid and throwOnError is true.
40
98
  */
41
- declare const calculateVerifier: (rutBody: string) => string;
99
+ declare function calculateVerifier(rutBody: string): VerifierDigit;
100
+ declare function calculateVerifier(rutBody: string, options: {
101
+ throwOnError: false;
102
+ }): VerifierDigit | null;
103
+ declare function calculateVerifier(rutBody: string, options: {
104
+ throwOnError: true;
105
+ }): VerifierDigit;
106
+ declare function calculateVerifier(rutBody: string, options?: SafeOptions): VerifierDigit | null;
42
107
  /**
43
108
  * Validates a given RUT string, optionally with strict mode to also check for suspicious patterns.
44
- * @param {string} rut - The RUT string to validate.
45
- * @param {boolean} [strict=false] - Whether to use strict mode for validation.
109
+ * @param {unknown} rut - The RUT string to validate.
110
+ * @param {ValidateOptions} [options] - Validation options.
111
+ * @param {boolean} [options.strict=false] - If true, rejects suspicious RUTs (e.g., 11.111.111-1).
46
112
  * @returns {boolean} True if the RUT is valid, false otherwise.
47
113
  */
48
- declare const validate: (rut: string, strict?: boolean) => boolean;
114
+ declare const validate: (rut: unknown, options?: ValidateOptions) => boolean;
49
115
  /**
50
116
  * Formats a given RUT string, with options for incremental formatting and dot separators.
117
+ *
118
+ * **Note on incremental mode**: When `incremental: true`, the function formats the RUT progressively
119
+ * as the user types, even for incomplete RUTs. This is useful for real-time formatting in form inputs,
120
+ * but the formatted output may not represent a valid RUT until the input is complete.
121
+ * Always use `validate()` to verify the final RUT.
122
+ *
51
123
  * @param {string} rut - The RUT string to format.
52
- * @param {boolean} [incremental=false] - Whether to format the RUT incrementally.
53
- * @param {boolean} [dots=true] - Whether to include dot separators in the formatted RUT.
54
- * @returns {string} The formatted RUT string.
124
+ * @param {FormatOptions} [options] - The formatting options.
125
+ * @param {boolean} [options.incremental=false] - Whether to format the RUT incrementally (for real-time input formatting).
126
+ * @param {boolean} [options.dots=true] - Whether to include dot separators in the formatted RUT.
127
+ * @param {boolean} [options.throwOnError=true] - If true (default), throws an error for invalid RUTs. If false, returns null. Ignored in incremental mode.
128
+ * @returns {string | null} The formatted RUT string, or null if invalid and throwOnError is false.
129
+ * @throws {Error} If the RUT is invalid and throwOnError is true (only in non-incremental mode).
55
130
  */
56
- declare const format: (rut: string, incremental?: boolean, dots?: boolean) => string;
131
+ declare function format(rut: string, options?: Omit<FormatOptions, 'throwOnError'>): string;
132
+ declare function format(rut: string, options: FormatOptions & {
133
+ throwOnError: false;
134
+ }): string | null;
135
+ declare function format(rut: string, options: FormatOptions & {
136
+ throwOnError: true;
137
+ }): string;
138
+ declare function format(rut: string, options?: FormatOptions): string | null;
57
139
  /**
58
140
  * Generates a random valid RUT string.
59
141
  * @returns {string} A randomly generated, valid RUT string.
60
142
  */
61
143
  declare const generate: () => string;
62
- export { validate, clean, format, calculateVerifier, getBody, getVerifier, decompose, generate };
144
+ export { validate, clean, format, calculateVerifier, getBody, getVerifier, decompose, generate, isRutLike };
145
+ export type { DecomposedRut, FormatOptions, SafeOptions, ValidateOptions, VerifierDigit };
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC,WAAW,GAAG,+BAA+B,CAAA;AAExG,MAAM,cAAc,GAAG,CAAC,CAAA;AACxB,MAAM,cAAc,GAAG,CAAC,CAAA;AAExB,MAAM,QAAQ,GAAuB;IACnC,QAAQ,EAAE,gBAAgB;IAC1B,OAAO,EAAE,oCAAoC;IAC7C,UAAU,EAAE,wCAAwC;CACrD,CAAA;AAED;;;;;GAKG;AACH,MAAM,KAAK,GAAG,CAAC,GAAW,EAAU,EAAE;IACpC,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,CAAA;IACjE,IAAI,QAAQ,CAAC,MAAM,GAAG,cAAc,IAAI,QAAQ,CAAC,MAAM,GAAG,cAAc;QAAE,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAA;IAClH,IAAI,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,QAAQ,CAAC,MAAM,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AE,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAA"}
@@ -1 +1 @@
1
- export const getInvalidRutError=e=>`String "${e}" is not valid as a RUT input`;const MIN_RUT_LENGTH=8,MAX_RUT_LENGTH=9,patterns={cleaning:/^0+|[^0-9kK]+/g,rutLike:/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/,suspicious:/^(\d)\1?\.?(\1{3})\.?(\1{3})-?(\d|k)?$/},clean=e=>{const t=e.replace(patterns.cleaning,"").toUpperCase();if(t.length<8||t.length>9)throw new Error(getInvalidRutError(e));if(t.includes("K")&&t.indexOf("K")!==t.length-1)throw new Error(getInvalidRutError(e));return t},getBody=e=>clean(e).slice(0,-1),getVerifier=e=>clean(e).slice(-1),decompose=e=>({body:getBody(e),verifier:getVerifier(e)}),isRutLike=e=>patterns.rutLike.test(e),isSuspicious=e=>patterns.suspicious.test(e),calculateVerifier=e=>{const t=clean(e);if(0===t.length)throw new Error(getInvalidRutError(e));const r=11-t.split("").reverse().reduce(((e,t,r)=>e+Number(t)*(r%6+2)),0)%11;return 11===r?"0":10===r?"K":r.toString()},validate=(e,t)=>{if(!isRutLike(e))return!1;if(t&&isSuspicious(e))return!1;const{body:r,verifier:i}=decompose(e);return calculateVerifier(r)===i},format=(e,t=!1,r=!0)=>{if(0===e.length)return"";if(e.length<=6&&!r)return e;const i=clean(e);if(t){let e=i.slice(-1);i.length>1&&(e=i.slice(-4,-1)+"-"+e);for(let t=4;t<i.length;t+=3){const n=i.length-3-t<0?0:i.length-3-t;e=r?i.slice(n,i.length-t)+"."+e:i.slice(n,i.length-t)+e}return e}if(r){let e=i.slice(-4,-1)+"-"+i.substring(i.length-1);for(let t=4;t<i.length;t+=3)e=i.slice(-3-t,-t)+"."+e;return e}return i.slice(0,-1)+"-"+i.substring(i.length-1)},generate=()=>{const e=Math.floor(10000003+9e7*Math.random()).toString(),t=calculateVerifier(e);return format(e+t)};export{validate,clean,format,calculateVerifier,getBody,getVerifier,decompose,generate};
1
+ export const getInvalidRutError=r=>`String "${r}" is not valid as a RUT input`;const withThrowOption=r=>({throwOnError:null==r||r}),MIN_RUT_LENGTH=8,MAX_RUT_LENGTH=9,cleanRaw=r=>r.replace(/^0+|[^0-9kK]+/g,"").toUpperCase(),patterns={cleaning:/^0+|[^0-9kK]+/g,rutLike:/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/,suspicious:/^(\d)\1?\.?(\1{3})\.?(\1{3})-?(\d|k)?$/};function clean(r,t){var n;const e=null===(n=null==t?void 0:t.throwOnError)||void 0===n||n,l=r.replace(patterns.cleaning,"").toUpperCase();if(l.length<MIN_RUT_LENGTH||l.length>MAX_RUT_LENGTH){if(e)throw new Error(getInvalidRutError(r));return null}if(l.includes("K")&&l.indexOf("K")!==l.length-1){if(e)throw new Error(getInvalidRutError(r));return null}return l}function getBody(r,t){var n;const e=clean(r,withThrowOption(null==t?void 0:t.throwOnError));return null!==(n=null==e?void 0:e.slice(0,-1))&&void 0!==n?n:null}function getVerifier(r,t){var n;const e=clean(r,withThrowOption(null==t?void 0:t.throwOnError));return null!==(n=null==e?void 0:e.slice(-1))&&void 0!==n?n:null}function decompose(r,t){const n=withThrowOption(null==t?void 0:t.throwOnError),e=getBody(r,n),l=getVerifier(r,n);return null===e||null===l?null:{body:e,verifier:l}}const isRutLike=r=>patterns.rutLike.test(r),isSuspicious=r=>patterns.suspicious.test(r);function calculateVerifier(r,t){const n=withThrowOption(null==t?void 0:t.throwOnError),e=cleanRaw(r);if(e.length<7||e.length>8){if(n.throwOnError)throw new Error(getInvalidRutError(r));return null}if(!/^\d+$/.test(e)){if(n.throwOnError)throw new Error(getInvalidRutError(r));return null}const l=11-e.split("").reverse().reduce(((r,t,n)=>r+Number(t)*(n%6+2)),0)%11;return 11===l?"0":10===l?"K":l.toString()}const validate=(r,t)=>{if("string"!=typeof r||0===r.length)return!1;if(!isRutLike(r))return!1;if((null==t?void 0:t.strict)&&isSuspicious(r))return!1;const n=decompose(r,{throwOnError:!1});if(!n)return!1;const e=calculateVerifier(n.body,{throwOnError:!1});return!!e&&e===n.verifier};function format(r,t){var n,e,l;const o={incremental:null!==(n=null==t?void 0:t.incremental)&&void 0!==n&&n,dots:null===(e=null==t?void 0:t.dots)||void 0===e||e,throwOnError:null===(l=null==t?void 0:t.throwOnError)||void 0===l||l};if(0===r.length)return"";if(o.incremental){const t=cleanRaw(r);if(0===t.length)return"";if(t.length<MIN_RUT_LENGTH){if(!o.dots||t.length<=3)return t;let r=t.slice(-3);for(let n=3;n<t.length;n+=3){const e=t.length-3-n<0?0:t.length-3-n;r=t.slice(e,t.length-n)+"."+r}return r}let n=t.slice(-1);n=t.slice(-4,-1)+"-"+n;for(let r=4;r<t.length;r+=3){const e=t.length-3-r<0?0:t.length-3-r;n=o.dots?t.slice(e,t.length-r)+"."+n:t.slice(e,t.length-r)+n}return n}const i=clean(r,withThrowOption(o.throwOnError));if(null===i)return null;if(o.dots){let r=i.slice(-4,-1)+"-"+i.substring(i.length-1);for(let t=4;t<i.length;t+=3)r=i.slice(-3-t,-t)+"."+r;return r}return i.slice(0,-1)+"-"+i.substring(i.length-1)}const generate=()=>{const r=Math.floor(10000003+9e7*Math.random()).toString();return format(r+calculateVerifier(r))};export{validate,clean,format,calculateVerifier,getBody,getVerifier,decompose,generate,isRutLike};
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "rut.ts",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "Handle chilean RUT values with ease.",
5
- "author": "hansfpc",
5
+ "author": "arrowsw",
6
6
  "main": "./dist/cjs/index.min.js",
7
7
  "module": "./dist/esm/index.min.js",
8
8
  "types": "./dist/esm/index.d.ts",
@@ -22,20 +22,20 @@
22
22
  },
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
- "@next/eslint-plugin-next": "14.2.3",
26
- "@types/jest": "29.5.12",
27
- "@typescript-eslint/eslint-plugin": "7.7.1",
28
- "@typescript-eslint/parser": "7.7.1",
29
- "eslint": "8.57.0",
30
- "eslint-config-prettier": "9.1.0",
31
- "eslint-plugin-prettier": "5.1.3",
32
- "eslint-plugin-react": "7.34.1",
33
- "eslint-plugin-react-hooks": "4.6.0",
34
- "jest": "29.7.0",
35
- "prettier": "3.2.5",
36
- "terser": "5.31.0",
37
- "ts-jest": "29.1.2",
38
- "typescript": "5.4.5"
25
+ "@next/eslint-plugin-next": "16.1.4",
26
+ "@types/jest": "30.0.0",
27
+ "@typescript-eslint/eslint-plugin": "8.53.1",
28
+ "@typescript-eslint/parser": "8.53.1",
29
+ "eslint": "9.39.2",
30
+ "eslint-config-prettier": "10.1.8",
31
+ "eslint-plugin-prettier": "5.5.5",
32
+ "eslint-plugin-react": "7.37.5",
33
+ "eslint-plugin-react-hooks": "7.0.1",
34
+ "jest": "30.2.0",
35
+ "prettier": "3.8.1",
36
+ "terser": "5.46.0",
37
+ "ts-jest": "29.4.6",
38
+ "typescript": "5.9.3"
39
39
  },
40
40
  "keywords": [
41
41
  "validation",
@@ -58,4 +58,4 @@
58
58
  "LICENSE",
59
59
  "README.md"
60
60
  ]
61
- }
61
+ }