states-alg
Version:
This package offers all Algerian states and departments and the distance between them and the other states, as well as some information about each state
88 lines (78 loc) • 2.32 kB
JavaScript
const { states } = require("./Data");
/**
* Find the state in the states array that has the same id as the id passed in as an argument.
* @param id - The ID of the state you want to get.
*/
const getWByID = (id) => states.find((x) => x.id == id);
/**
* It takes a name in Arabic and returns the corresponding state object.
* @param nameAR - The name of the state in Arabic.
*/
const getWNameAR = (nameAR) => states.find((x) => x.nameAR == nameAR);
/**
* It takes a string of two letters, and returns the state object that matches the two letters
* @param ascii - The two-letter abbreviation for the state.
*/
const getWByASCII = (ascii) => states.find((x) => x.nameASCII == ascii);
/**
* It returns an array of all the names of the states in Arabic.
* @returns An array of strings.
*/
const getAllWillayaAR = () => {
return states.map((a) => a.nameAR);
};
/**
* It returns an array of all the ASCII names of the states.
* @returns An array of the ASCII names of the states.
*/
const getAllWillayaASCII = () => {
return states.map((a) => a.nameASCII);
};
/* A function that takes a name in Arabic and returns the corresponding state object. */
const getDairaArabic = (nameAR) => {
let arr = null;
for (let i = 0; i < states.length; i++) {
for (let j = 0; j < states[i].daira.length; j++) {
if (states[i].daira[j].dairaNameAR === nameAR) {
arr = states[i];
break;
}
}
if (arr != null) break;
}
return {
nameASCII: arr.nameAR,
countDaira: arr.daira.length,
extra: arr,
};
};
const getDairaASCII = (nameASCII) => {
let arr = null;
for (let i = 0; i < states.length; i++) {
for (let j = 0; j < states[i].daira.length; j++) {
if (states[i].daira[j].dairaNameASCII === nameASCII) {
arr = states[i];
break;
}
}
if (arr != null) break;
}
return {
nameASCII: arr.nameAR,
countDaira: arr.daira.length,
extra: arr,
};
};
/**
* It takes a name in Arabic and returns the corresponding state object.
* @param nameAR - The name of the state in Arabic.
*/
module.exports = {
getWByID,
getWNameAR,
getWByASCII,
getAllWillayaAR,
getAllWillayaASCII,
getDairaASCII,
getDairaArabic,
};