-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(index): add typescript to the project
- Loading branch information
Showing
6 changed files
with
681 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export declare const validRutValue: (value: string) => boolean; | ||
export declare const stringifyValue: (value: string | number) => string | undefined; | ||
export declare const validRutValues: (rut: string) => boolean; | ||
export declare const normalizeRut: (rut: string) => string | undefined; | ||
export declare const getRutVerifier: (rut: string) => string | undefined; | ||
export declare const translateVerifierResult: (result: number) => string; | ||
export declare const validRutVerifier: (rut: string) => boolean | undefined; | ||
export declare const validRut: (rut: string) => boolean | undefined; | ||
export declare const formatRut: (rut: string) => string | undefined; | ||
declare const _default: { | ||
validRutValue: (value: string) => boolean; | ||
stringifyValue: (value: string | number) => string | undefined; | ||
validRutValues: (rut: string) => boolean; | ||
normalizeRut: (rut: string) => string | undefined; | ||
getRutVerifier: (rut: string) => string | undefined; | ||
translateVerifierResult: (result: number) => string; | ||
validRutVerifier: (rut: string) => boolean | undefined; | ||
validRut: (rut: string) => boolean | undefined; | ||
formatRut: (rut: string) => string | undefined; | ||
}; | ||
export default _default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,161 +1,106 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.validRutVerifier = exports.validRutValues = exports.validRutValue = exports.validRut = exports.translateVerifierResult = exports.stringifyValue = exports.normalizeRut = exports.getRutVerifier = exports.formatRut = exports["default"] = void 0; | ||
|
||
// validates that value can be used in a rut | ||
var validRutValue = function validRutValue(value) { | ||
value = stringifyValue(value); | ||
var validValues = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "k", "K", ".", "-"]; | ||
return validValues.includes(value); | ||
}; //returns a string if posible | ||
|
||
|
||
exports.validRutValue = validRutValue; | ||
|
||
var stringifyValue = function stringifyValue(value) { | ||
if (typeof value === "string" || typeof value === "number") { | ||
return value.toString(); | ||
} | ||
|
||
return; | ||
}; // validates if rut values are valid | ||
|
||
|
||
exports.stringifyValue = stringifyValue; | ||
|
||
var validRutValues = function validRutValues(rut) { | ||
rut = stringifyValue(rut); | ||
|
||
if (!rut) { | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < rut.length; i++) { | ||
if (!validRutValue(rut[i])) { | ||
return false; | ||
export const validRutValue = (value) => { | ||
value = stringifyValue(value); | ||
const validValues = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "k", "K", ".", "-"]; | ||
return validValues.includes(value); | ||
}; | ||
//returns a string if posible | ||
export const stringifyValue = (value) => { | ||
if (typeof value === "string" || typeof value === "number") { | ||
return value.toString(); | ||
} | ||
} | ||
|
||
return true; | ||
}; // returns rut without dots or dashes | ||
|
||
|
||
exports.validRutValues = validRutValues; | ||
|
||
var normalizeRut = function normalizeRut(rut) { | ||
rut = stringifyValue(rut); | ||
|
||
if (!rut || !validRutValues(rut)) { | ||
return; | ||
} | ||
|
||
rut = rut.replace(/[.-]/g, ""); | ||
return rut.toUpperCase(); | ||
}; // get the rut verifier digit | ||
|
||
|
||
exports.normalizeRut = normalizeRut; | ||
|
||
var getRutVerifier = function getRutVerifier(rut) { | ||
rut = normalizeRut(rut); | ||
|
||
if (!rut) { | ||
return; | ||
} | ||
|
||
var sum = 0; | ||
var mul = 2; | ||
|
||
for (var i = rut.length - 1; i >= 0; i--) { | ||
sum += parseInt(rut[i]) * mul; | ||
mul === 7 ? mul = 2 : mul++; | ||
} | ||
|
||
var res = sum % 11; | ||
return translateVerifierResult(res); | ||
}; // translate rut verifier digit | ||
|
||
|
||
exports.getRutVerifier = getRutVerifier; | ||
|
||
var translateVerifierResult = function translateVerifierResult(result) { | ||
if (result === 0) { | ||
return "0"; | ||
} | ||
|
||
if (result === 1) { | ||
return "K"; | ||
} | ||
|
||
return (11 - result).toString(); | ||
}; // validates rut verifier digit | ||
|
||
|
||
exports.translateVerifierResult = translateVerifierResult; | ||
|
||
var validRutVerifier = function validRutVerifier(rut) { | ||
rut = normalizeRut(rut); | ||
|
||
if (!rut) { | ||
return; | ||
} | ||
|
||
var verifier = rut.slice(-1); | ||
var rutValue = rut.slice(0, -1); | ||
return verifier === getRutVerifier(rutValue); | ||
}; // validates rut | ||
|
||
|
||
exports.validRutVerifier = validRutVerifier; | ||
|
||
var validRut = function validRut(rut) { | ||
rut = normalizeRut(rut); | ||
|
||
if (!rut) { | ||
return; | ||
} | ||
|
||
return validRutValues(rut) && validRutVerifier(rut); | ||
}; // formats rut | ||
|
||
|
||
exports.validRut = validRut; | ||
|
||
var formatRut = function formatRut(rut) { | ||
rut = normalizeRut(rut); | ||
|
||
if (!rut) { | ||
return; | ||
} | ||
|
||
var verifier = rut.slice(-1); | ||
var rutValue = rut.slice(0, -1); | ||
var rutFormated = ""; | ||
|
||
for (var i = rutValue.length - 1; i >= 0; i--) { | ||
rutFormated = rutValue[i] + rutFormated; | ||
|
||
if ((rutValue.length - i) % 3 === 0 && i !== 0) { | ||
rutFormated = "." + rutFormated; | ||
}; | ||
// validates if rut values are valid | ||
export const validRutValues = (rut) => { | ||
rut = stringifyValue(rut); | ||
if (!rut) { | ||
return false; | ||
} | ||
for (const element of rut) { | ||
if (!validRutValue(element)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
// returns rut without dots or dashes | ||
export const normalizeRut = (rut) => { | ||
rut = stringifyValue(rut); | ||
if (!rut || !validRutValues(rut)) { | ||
return; | ||
} | ||
rut = rut.replace(/[.-]/g, ""); | ||
return rut.toUpperCase(); | ||
}; | ||
// get the rut verifier digit | ||
export const getRutVerifier = (rut) => { | ||
rut = normalizeRut(rut); | ||
if (!rut) { | ||
return; | ||
} | ||
let sum = 0; | ||
let mul = 2; | ||
for (let i = rut.length - 1; i >= 0; i--) { | ||
sum += parseInt(rut[i]) * mul; | ||
mul === 7 ? mul = 2 : mul++; | ||
} | ||
const res = sum % 11; | ||
return translateVerifierResult(res); | ||
}; | ||
// translate rut verifier digit | ||
export const translateVerifierResult = (result) => { | ||
if (result === 0) { | ||
return "0"; | ||
} | ||
if (result === 1) { | ||
return "K"; | ||
} | ||
return (11 - result).toString(); | ||
}; | ||
// validates rut verifier digit | ||
export const validRutVerifier = (rut) => { | ||
rut = normalizeRut(rut); | ||
if (!rut) { | ||
return; | ||
} | ||
const verifier = rut.slice(-1); | ||
const rutValue = rut.slice(0, -1); | ||
return verifier === getRutVerifier(rutValue); | ||
}; | ||
// validates rut | ||
export const validRut = (rut) => { | ||
rut = normalizeRut(rut); | ||
if (!rut) { | ||
return; | ||
} | ||
return validRutValues(rut) && validRutVerifier(rut); | ||
}; | ||
// formats rut | ||
export const formatRut = (rut) => { | ||
rut = normalizeRut(rut); | ||
if (!rut) { | ||
return; | ||
} | ||
const verifier = rut.slice(-1); | ||
const rutValue = rut.slice(0, -1); | ||
let rutFormated = ""; | ||
for (let i = rutValue.length - 1; i >= 0; i--) { | ||
rutFormated = rutValue[i] + rutFormated; | ||
if ((rutValue.length - i) % 3 === 0 && i !== 0) { | ||
rutFormated = "." + rutFormated; | ||
} | ||
} | ||
} | ||
|
||
return rutFormated + "-" + verifier; | ||
return rutFormated + "-" + verifier; | ||
}; | ||
|
||
exports.formatRut = formatRut; | ||
var _default = { | ||
validRutValue: validRutValue, | ||
stringifyValue: stringifyValue, | ||
validRutValues: validRutValues, | ||
normalizeRut: normalizeRut, | ||
getRutVerifier: getRutVerifier, | ||
translateVerifierResult: translateVerifierResult, | ||
validRutVerifier: validRutVerifier, | ||
validRut: validRut, | ||
formatRut: formatRut | ||
export default { | ||
validRutValue, | ||
stringifyValue, | ||
validRutValues, | ||
normalizeRut, | ||
getRutVerifier, | ||
translateVerifierResult, | ||
validRutVerifier, | ||
validRut, | ||
formatRut | ||
}; | ||
exports["default"] = _default; |
Oops, something went wrong.