main
parent
eafd71300d
commit
3c3dd7a95f
181 changed files with 28818 additions and 84 deletions
@ -1 +1 @@ |
|||||||
{"version":3,"sources":["game.css","game.scss"],"names":[],"mappings":"AAAA,gBAAgB;ACAhB;EACI,kBAAA;EACA,QAAA;EACA,SAAA;EACA,oBAAA;ADEJ;ACDI;EACI,kBAAA;EACA,eAAA;EACA,YAAA;EACA,qBAAA;ADGR;ACDI;EAEI,6BAAA;EACA,uCAAA;EACA,kBAAA;EACA,oBAAA;EACA,kBAAA;EACA,YAAA;ADER;ACDQ;EACI,cAAA;EACA,YAAA;EACA,iBAAA;EACA,eAAA;EACA,uBAAA;EACA,iBAAA;EACA,kBAAA;ADGZ;ACDQ;EACI,cAAA;EACA,YAAA;EACA,kBAAA;EACA,eAAA;EACA,YAAA;EACA,iBAAA;EACA,kBAAA;ADGZ;ACDQ;EACI,YAAA;EACA,eAAA;EACA,kBAAA;ADGZ;ACDQ;EACI,aAAA;EACA,YAAA;EACA,iBAAA;EACA,iBAAA;EACA,kBAAA;EACA,gBAAA;EACA,8BAAA;EACA,yBAAA;EACA,mBAAA;EACA,oCAAA;ADGZ;;ACKA;EAGI,WAAA;EACA,YAAA;EACA,kBAAA;ADJJ;ACMI;EAEI,WAAA;EACA,kBAAA;EACA,eAAA;EACA,2BAAA;EACA,qBAAA;EACA,4BAAA;EACA,yBAAA;EACA,8BAAA;EACA,kEAAA;EACA,UAAA;EACA,kBAAA;EAEA,eAAA;ADNR;ACOQ;EACI,qCAAA;EACA,0CAAA;ADLZ;ACQY;EACI,4DAAA;ADNhB;ACQY;EACI,4DAAA;ADNhB;ACWY;EACI,4DAAA;ADThB;ACWY;EACI,4DAAA;ADThB;ACaQ;EAKI,mBAAA;EACA,6BAAA;EACA,uCAAA;EACA,YAAA;EACA,eAAA;EACA,kBAAA;EACA,kBAAA;EACA,kBAAA;EACA,YAAA;EACA,QAAA;EACA,SAAA;EACA,eAAA;EACA,oBAAA;EACA,UAAA;EACA,gBAAA;ADfZ;ACHY;EACI,mCAAA;ADKhB;ACgBY;EACI,UAAA;ADdhB;;ACqBI;EACI,6BAAA;EACA,aAAA;EACA,wCAAA;EACA,YAAA;EACA,kBAAA;EACA,uBAAA;EAAA,kBAAA;EACA,kBAAA;ADlBR;ACmBQ;EACI,qBAAA;ADjBZ;ACkBY;EACI,kBAAA;EACA,WAAA;EACA,kBAAA;EACA,eAAA;EACA,WAAA;EACA,eAAA;EACA,iBAAA;ADhBhB;ACkBgB;EACI,iBAAA;EACA,qDAAA;ADhBpB;ACoBgB;EACI,iBAAA;EACA,qDAAA;ADlBpB;ACoBgB;EACI,iBAAA;ADlBpB;;AC2BA;EACI;IAAK,wBAAA;EDvBP;ECwBE;IAAM,0BAAA;EDrBR;ECsBE;IAAM,2BAAA;EDnBR;ECoBE;IAAM,0BAAA;EDjBR;ECkBE;IAAO,wBAAA;EDfT;AACF","file":"game.css"} |
{"version":3,"sources":["game.css","game.scss"],"names":[],"mappings":"AAAA,gBAAgB;ACAhB;EACI,kBAAA;EACA,QAAA;EACA,SAAA;EACA,oBAAA;ADEJ;ACDI;EACI,kBAAA;EACA,eAAA;EACA,YAAA;EACA,qBAAA;ADGR;ACDI;EAEI,6BAAA;EACA,uCAAA;EACA,kBAAA;EACA,oBAAA;EACA,kBAAA;EACA,aAAA;ADER;ACDQ;EACI,cAAA;EACA,YAAA;EACA,iBAAA;EACA,eAAA;EACA,uBAAA;EACA,iBAAA;EACA,kBAAA;ADGZ;ACDQ;EACI,cAAA;EACA,YAAA;EACA,kBAAA;EACA,eAAA;EACA,YAAA;EACA,iBAAA;EACA,kBAAA;ADGZ;ACDQ;EACI,YAAA;EACA,eAAA;EACA,kBAAA;ADGZ;ACDQ;EACI,aAAA;EACA,YAAA;EACA,iBAAA;EACA,iBAAA;EACA,kBAAA;EACA,gBAAA;EACA,8BAAA;EACA,yBAAA;EACA,mBAAA;EACA,oCAAA;ADGZ;;ACKA;EAGI,WAAA;EACA,YAAA;EACA,kBAAA;ADJJ;ACMI;EAEI,WAAA;EACA,kBAAA;EACA,eAAA;EACA,2BAAA;EACA,qBAAA;EACA,4BAAA;EACA,yBAAA;EACA,8BAAA;EACA,kEAAA;EACA,UAAA;EACA,kBAAA;EAEA,eAAA;ADNR;ACOQ;EACI,qCAAA;EACA,0CAAA;ADLZ;ACQY;EACI,4DAAA;ADNhB;ACQY;EACI,4DAAA;ADNhB;ACWY;EACI,4DAAA;ADThB;ACWY;EACI,4DAAA;ADThB;ACaQ;EAKI,mBAAA;EACA,6BAAA;EACA,uCAAA;EACA,YAAA;EACA,eAAA;EACA,kBAAA;EACA,kBAAA;EACA,kBAAA;EACA,YAAA;EACA,QAAA;EACA,SAAA;EACA,eAAA;EACA,oBAAA;EACA,UAAA;EACA,gBAAA;ADfZ;ACHY;EACI,mCAAA;ADKhB;ACgBY;EACI,UAAA;ADdhB;;ACqBI;EACI,6BAAA;EACA,aAAA;EACA,wCAAA;EACA,YAAA;EACA,kBAAA;EACA,uBAAA;EAAA,kBAAA;EACA,kBAAA;ADlBR;ACmBQ;EACI,qBAAA;ADjBZ;ACkBY;EACI,kBAAA;EACA,WAAA;EACA,kBAAA;EACA,eAAA;EACA,WAAA;EACA,eAAA;EACA,iBAAA;ADhBhB;ACkBgB;EACI,iBAAA;EACA,qDAAA;ADhBpB;ACoBgB;EACI,iBAAA;EACA,qDAAA;ADlBpB;ACoBgB;EACI,iBAAA;ADlBpB;;AC2BA;EACI;IAAK,wBAAA;EDvBP;ECwBE;IAAM,0BAAA;EDrBR;ECsBE;IAAM,2BAAA;EDnBR;ECoBE;IAAM,0BAAA;EDjBR;ECkBE;IAAO,wBAAA;EDfT;AACF","file":"game.css"} |
||||||
@ -1,70 +0,0 @@ |
|||||||
<!DOCTYPE html> |
|
||||||
<html lang="en"> |
|
||||||
<head> |
|
||||||
<meta charset="UTF-8"> |
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
||||||
<title>Dáma Online</title> |
|
||||||
<link rel="stylesheet" href="./css/game.css"> |
|
||||||
<link rel="stylesheet" href="./css/main.css"> |
|
||||||
</head> |
|
||||||
<body> |
|
||||||
|
|
||||||
<section id="game" class="invalid-move"> |
|
||||||
<div id="pieces-area"></div> |
|
||||||
<div id="playfield"></div> |
|
||||||
</section> |
|
||||||
<section id="menu"> |
|
||||||
<h1>Dáma online</h1> |
|
||||||
<div class="box" id="main-menu"> |
|
||||||
<input type="text" name="nickname" id="nickname" placeholder="Zvoj si jméno"> |
|
||||||
<button>Začít Hrát</button> |
|
||||||
<button>Založit místnost</button> |
|
||||||
<button>Připojit do místnosti</button> |
|
||||||
<button>Lokální Hra</button> |
|
||||||
<button>Jak hrát?</button> |
|
||||||
</div> |
|
||||||
<div class="box" id="loader" style="display: none;"> |
|
||||||
<h2>Hledám hru</h2> |
|
||||||
<div class="loader"> |
|
||||||
<div></div> |
|
||||||
<div></div> |
|
||||||
<div></div> |
|
||||||
<div></div> |
|
||||||
<div></div> |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
<div class="box" id="code-entry" style="display: none;"> |
|
||||||
<h2>Připojit se</h2> |
|
||||||
<input type="text" name="code" id="code" placeholder="kód hry"> |
|
||||||
<button>potvrdit</button> |
|
||||||
<button>zpět</button> |
|
||||||
</div> |
|
||||||
<div class="box error" id="alert" style="display: none;"> |
|
||||||
<h2>Chyba!</h2> |
|
||||||
<p>Server neodpovídá: Lorem, ipsum dolor sit amet consectetur adipisicing elit. Repudiandae eligendi, quaerat dolorem, odio cum quidem aliquam fugit tempora nesciunt tempore, accusantium ex officiis eum quo mollitia vel maxime iure dolores? |
|
||||||
Officiis sit maiores architecto recusandae esse animi voluptatum omnis labore. Reiciendis doloremque consequatur praesentium ipsum ab deleniti a sit numquam soluta illo perferendis repellat rem, autem saepe ad beatae quibusdam. |
|
||||||
Maxime, voluptas similique magnam dolorum optio nobis eligendi. Voluptatum iure delectus, alias aut, facilis repudiandae molestiae, accusantium dolorem cumque voluptates suscipit dignissimos temporibus sequi! Voluptatum autem quas doloribus quisquam iure. |
|
||||||
Reiciendis molestias est at earum, blanditiis unde repudiandae. Saepe atque dolore, labore nihil debitis itaque commodi veritatis perferendis sunt. Quas atque praesentium dicta soluta ratione exercitationem animi labore neque? Facilis. |
|
||||||
Sapiente consequatur, eveniet earum debitis accusantium exercitationem. Id quod perspiciatis nesciunt optio fugit accusantium sequi qui ea excepturi quae veritatis quos, ullam animi libero aut aspernatur asperiores consequuntur labore quam! |
|
||||||
Totam a accusantium fugit animi minus minima consectetur libero velit modi in molestiae temporibus id illo ab reprehenderit assumenda voluptas, soluta quis dolorem veniam enim eaque dolores facilis quaerat! Architecto. |
|
||||||
Ab quas dolores nulla quos architecto voluptas deleniti nobis voluptate quaerat maxime. Nihil dolor adipisci accusantium enim. Possimus vero, harum ab adipisci eligendi deserunt nisi ad dolor praesentium cupiditate! Quo. |
|
||||||
Praesentium aliquam quae culpa nisi delectus magni deleniti tempora corporis atque quos, eius reiciendis quas, commodi nostrum dicta temporibus minima quasi fugiat. Consequuntur labore aliquam rem quae voluptatibus quam mollitia. |
|
||||||
Odio blanditiis sit, culpa voluptates aut labore! Incidunt, earum necessitatibus libero assumenda magnam est odit rem ex minus. Commodi nobis autem veritatis aperiam rerum nulla dignissimos quaerat tempora dolor obcaecati! |
|
||||||
Eveniet, molestias rerum officiis aliquid necessitatibus eaque veniam, commodi dolor nemo sint voluptas explicabo voluptatum, temporibus quasi quis illum architecto. Ea beatae odio facere? Ipsum eos sequi officia natus nam!</p> |
|
||||||
<button>zpět</button> |
|
||||||
</div> |
|
||||||
<div class="box lobby"> |
|
||||||
<h2>Soukromá Hra</h2> |
|
||||||
<p>Zvací kód: <span>A5G3D</span></p> |
|
||||||
<p>Hráč bílý: <span>Test</span></p> |
|
||||||
<p>Hráč černý: <span>-</span></p> |
|
||||||
<button>Prohodit hráče</button> |
|
||||||
<button disabled>začít hru</button> |
|
||||||
</div> |
|
||||||
</section> |
|
||||||
<script src="https://cdn.socket.io/4.8.1/socket.io.min.js"></script> |
|
||||||
<script src="./javascript/game-logic.js"></script> |
|
||||||
<script src="./javascript/communication.js"></script> |
|
||||||
<script src="./javascript/client.js"></script> |
|
||||||
</body> |
|
||||||
</html> |
|
||||||
@ -0,0 +1,187 @@ |
|||||||
|
const playField = document.getElementById("playfield"); |
||||||
|
const piecesArea = document.getElementById("pieces-area"); |
||||||
|
const socket = io("ws://localhost:3000"); |
||||||
|
player = "white"; |
||||||
|
selectedPiece = null; |
||||||
|
moveContinuation = false; |
||||||
|
userName = ""; |
||||||
|
oponentName = null; |
||||||
|
|
||||||
|
function movePiece(piece, x, y){ |
||||||
|
|
||||||
|
const tile = document.getElementById("tile" + x + y); |
||||||
|
|
||||||
|
const offsetPosition = piecesArea.getBoundingClientRect(); |
||||||
|
const position = tile.getBoundingClientRect(); |
||||||
|
|
||||||
|
const top = position.top - offsetPosition.top; |
||||||
|
const left = position.left - offsetPosition.left;
|
||||||
|
|
||||||
|
piece.style.top = top + "px"; |
||||||
|
piece.style.left = left + "px"; |
||||||
|
|
||||||
|
piece.setAttribute("x", x); |
||||||
|
piece.setAttribute("y", y); |
||||||
|
} |
||||||
|
|
||||||
|
function generatePlayField(){ |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for (let i = 0; i < 8; i++) { |
||||||
|
|
||||||
|
row = document.createElement("div"); |
||||||
|
|
||||||
|
row.classList.add("row"); |
||||||
|
|
||||||
|
row.id = "row" + i; |
||||||
|
|
||||||
|
for (let j = 0; j < 8; j++) { |
||||||
|
tile = document.createElement("div"); |
||||||
|
|
||||||
|
tile.classList.add("tile"); |
||||||
|
tile.id = "tile" + i + j; |
||||||
|
tile.setAttribute("x", i); |
||||||
|
tile.setAttribute("y", j); |
||||||
|
|
||||||
|
row.appendChild(tile); |
||||||
|
|
||||||
|
tile.addEventListener("click", tileClickEvent.bind(null,i,j)); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
playField.appendChild(row); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
function generatePieces(){ |
||||||
|
pieceList.forEach(pieceRecord => { |
||||||
|
piece = document.createElement("div"); |
||||||
|
|
||||||
|
piece.classList.add(pieceRecord.player); |
||||||
|
piece.classList.add(pieceRecord.type); |
||||||
|
|
||||||
|
piece.setAttribute("x", pieceRecord.x); |
||||||
|
piece.setAttribute("y", pieceRecord.y); |
||||||
|
|
||||||
|
piece.id = "piece" + pieceRecord.id; |
||||||
|
|
||||||
|
piecesArea.appendChild(piece); |
||||||
|
|
||||||
|
movePiece(piece, pieceRecord.x, pieceRecord.y) |
||||||
|
|
||||||
|
|
||||||
|
piece.addEventListener("click", pieceClickEvent.bind("null", piece)) |
||||||
|
|
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
function getPieceRecordFromPiece(piece) { |
||||||
|
return getPieceRecord(piece.getAttribute("x"), piece.getAttribute("y")); |
||||||
|
} |
||||||
|
|
||||||
|
function updatePieces() { |
||||||
|
|
||||||
|
const safeList = new Array(); |
||||||
|
|
||||||
|
pieceList.forEach(pieceRecord => { |
||||||
|
|
||||||
|
const piece = document.getElementById("piece" + pieceRecord.id); |
||||||
|
const classList = piece.classList; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
classList.remove("basic"); |
||||||
|
classList.remove("queen"); |
||||||
|
classList.add(pieceRecord.type); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
safeList.push(piece.id); |
||||||
|
movePiece(piece, pieceRecord.x, pieceRecord.y); |
||||||
|
|
||||||
|
if (missedOportunities.includes(pieceRecord.id)) { |
||||||
|
alert("pepe"); |
||||||
|
addRemover(piece); |
||||||
|
}else{ |
||||||
|
piece.innerHTML = ""; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}); |
||||||
|
|
||||||
|
const pieces = piecesArea.childNodes; |
||||||
|
|
||||||
|
pieces.forEach(piece => { |
||||||
|
if (!safeList.includes(piece.id)) { |
||||||
|
piece.remove(); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
function resetPieceSelection() { |
||||||
|
if(selectedPiece != null){ |
||||||
|
selectedPiece.classList.remove("selected"); |
||||||
|
selectedPiece = null; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function pieceClickEvent(piece){ |
||||||
|
const pieceRecord = getPieceRecordFromPiece(piece); |
||||||
|
if(pieceRecord.player == player){ |
||||||
|
|
||||||
|
resetPieceSelection(); |
||||||
|
selectedPiece = piece; |
||||||
|
selectedPiece.classList.add("selected"); |
||||||
|
} |
||||||
|
if(piece.hasChildNodes() && nukePieceRecordThatMissedOportunity(pieceRecord)){ |
||||||
|
updatePieces(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
function tileClickEvent(x, y){ |
||||||
|
|
||||||
|
if(selectedPiece != null){ |
||||||
|
|
||||||
|
const pieceRecord = getPieceRecordFromPiece(selectedPiece); |
||||||
|
const validity = makeMove(pieceRecord, x,y); |
||||||
|
|
||||||
|
switch (validity) { |
||||||
|
case 0: |
||||||
|
alert("neplatný tah!") |
||||||
|
break; |
||||||
|
case 1: |
||||||
|
resetPieceSelection(); |
||||||
|
updatePieces(); |
||||||
|
player = (player == "white") ? "black" : "white"; |
||||||
|
break; |
||||||
|
case 2: |
||||||
|
updatePieces();
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/*movePiece(selectedPiece, x, y); |
||||||
|
resetPieceSelection();*/ |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function addRemover(piece) { |
||||||
|
const element = document.createElement("div"); |
||||||
|
element.classList.add("removable-tag"); |
||||||
|
piece.appendChild(element); |
||||||
|
} |
||||||
|
|
||||||
|
generatePlayField(); |
||||||
|
generatePieces(); |
||||||
|
|
||||||
|
|
||||||
@ -1,3 +0,0 @@ |
|||||||
//const socket = io("ws://localhost:3000");
|
|
||||||
document.getElementById("game").style.display = "none"; |
|
||||||
document.getElementById("main-menu").style.display = "none"; |
|
||||||
@ -0,0 +1,70 @@ |
|||||||
|
|
||||||
|
|
||||||
|
function show(id){ |
||||||
|
document.getElementById(id).style.display = "block"; |
||||||
|
} |
||||||
|
|
||||||
|
function hide(id){ |
||||||
|
document.getElementById(id).style.display = "none"; |
||||||
|
} |
||||||
|
|
||||||
|
function getUserName(){ |
||||||
|
if (userName == "") { |
||||||
|
userName = document.getElementById("nickname").value; |
||||||
|
} |
||||||
|
return userName; |
||||||
|
} |
||||||
|
|
||||||
|
function resetMenu(){ |
||||||
|
const children = document.getElementById("menu").children; |
||||||
|
Array.from(children).forEach(child => { |
||||||
|
if (child.tagName == "DIV") { |
||||||
|
child.style.display = "none"; |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
function setText(id, text){ |
||||||
|
document.getElementById(id).innerHTML = text; |
||||||
|
} |
||||||
|
|
||||||
|
function playLocal(){ |
||||||
|
location.href = "./local.html"; |
||||||
|
}
|
||||||
|
|
||||||
|
function createGame(){ |
||||||
|
|
||||||
|
if(getUserName() == ""){ |
||||||
|
alert("musíš zadat přezdívku"); |
||||||
|
return; |
||||||
|
} |
||||||
|
setText("loaderText", "Zakládám hru"); |
||||||
|
hide("main-menu"); |
||||||
|
show("loader"); |
||||||
|
socket.emit("createGameRequest", {userName}, (response) => { |
||||||
|
if(response.status == "success"){ |
||||||
|
hide("loader"); |
||||||
|
show("lobby"); |
||||||
|
}else{ |
||||||
|
alert("fck"); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
function joinWithCode(){ |
||||||
|
if(getUserName() == ""){ |
||||||
|
alert("musíš zadat přezdívku"); |
||||||
|
return; |
||||||
|
} |
||||||
|
hide("main-menu"); |
||||||
|
show("code-entry"); |
||||||
|
} |
||||||
|
|
||||||
|
function backToMainMenu(){ |
||||||
|
resetMenu(); |
||||||
|
show("main-menu"); |
||||||
|
} |
||||||
|
|
||||||
|
show("main-menu"); |
||||||
|
hide("game"); |
||||||
|
|
||||||
@ -0,0 +1,21 @@ |
|||||||
|
<!DOCTYPE html> |
||||||
|
<html lang="en"> |
||||||
|
<head> |
||||||
|
<meta charset="UTF-8"> |
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||||||
|
<title>Dáma Online</title> |
||||||
|
<link rel="stylesheet" href="./css/game.css"> |
||||||
|
<link rel="stylesheet" href="./css/main.css"> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
|
||||||
|
<section id="game" class="invalid-move"> |
||||||
|
|
||||||
|
<div id="pieces-area"></div> |
||||||
|
<div id="playfield"></div> |
||||||
|
</section> |
||||||
|
<script src="./javascript/game-logic.js"></script> |
||||||
|
<script src="./javascript/client-local.js"> |
||||||
|
</script> |
||||||
|
</body> |
||||||
|
</html> |
||||||
@ -0,0 +1,306 @@ |
|||||||
|
|
||||||
|
|
||||||
|
const pieceList = new Array(); |
||||||
|
missedOportunities = new Array(); |
||||||
|
currentPlayer = "white"; |
||||||
|
|
||||||
|
function isMissedOportunity(pieceRecord) { |
||||||
|
const id = pieceRecord.id; |
||||||
|
return missedOportunities.includes(id); |
||||||
|
} |
||||||
|
|
||||||
|
function generateDefaultPositions(){ |
||||||
|
|
||||||
|
var id = 0; |
||||||
|
|
||||||
|
for (let i = 0; i < 8; i++) { |
||||||
|
for (let j = 0; j < 3; j++) { |
||||||
|
if((i+j) % 2 == 0){ |
||||||
|
pieceRecord = {id:id, x:i, y:j, player:"white", type:"basic"}; |
||||||
|
pieceList.push(pieceRecord); |
||||||
|
id++; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for (let i = 0; i < 8; i++) { |
||||||
|
for (let j = 5; j < 8; j++) { |
||||||
|
if((i+j) % 2 == 0){ |
||||||
|
pieceRecord = {id:id, x:i, y:j, player:"black", type:"basic"}; |
||||||
|
pieceList.push(pieceRecord); |
||||||
|
id++; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function getPieceRecord(x,y){ |
||||||
|
if (x > 7 || x < 0 || y > 7 || y < 0) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
return pieceList.find(pieceRecord => pieceRecord.x == x && pieceRecord.y == y); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function nukePieceRecord(pieceRecord) { |
||||||
|
const index = pieceList.findIndex(record => record.id == pieceRecord.id); |
||||||
|
pieceList.splice(index, 1); |
||||||
|
} |
||||||
|
|
||||||
|
function updatePieceRecord(pieceRecord) { |
||||||
|
const index = pieceList.findIndex(record => record.id == pieceRecord.id); |
||||||
|
pieceList[index] = pieceRecord; |
||||||
|
} |
||||||
|
|
||||||
|
function updatePieceRecordPosition(pieceRecord, x, y) { |
||||||
|
pieceRecord.x = x; |
||||||
|
pieceRecord.y = y; |
||||||
|
updatePieceRecord(pieceRecord); |
||||||
|
return pieceRecord; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function getCasualties(oldX, oldY, newX, newY) { |
||||||
|
const directionX = (newX > oldX) ? 1 : -1; |
||||||
|
const directionY = (newY > oldY) ? 1 : -1; |
||||||
|
|
||||||
|
const casualties = new Array(); |
||||||
|
|
||||||
|
for (let i = 1; i < (Math.abs(newY - oldY) + 1); i++) { |
||||||
|
const x = oldX + (i*directionX); |
||||||
|
const y = oldY + (i*directionY); |
||||||
|
|
||||||
|
pieceRecord = getPieceRecord(x,y); |
||||||
|
|
||||||
|
if (pieceRecord) { |
||||||
|
casualties.push(pieceRecord); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
return casualties; |
||||||
|
} |
||||||
|
|
||||||
|
function getLoser() { |
||||||
|
if (!pieceList.find(pieceRecord => pieceRecord.player == "white")) { |
||||||
|
return "white"; |
||||||
|
} |
||||||
|
if (!pieceList.find(pieceRecord => pieceRecord.player == "black")) { |
||||||
|
return "black"; |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
function continuableInDirectionForQueen(directionX, directionY, pieceRecord) { |
||||||
|
|
||||||
|
const pieceX = pieceRecord.x; |
||||||
|
const pieceY = pieceRecord.y; |
||||||
|
const player = pieceRecord.player; |
||||||
|
|
||||||
|
for (let i = 1; i < 8; i++) { |
||||||
|
const x = pieceX + (i*directionX); |
||||||
|
const y = pieceY + (i*directionY); |
||||||
|
|
||||||
|
if(x > 7 || x < 0 || y > 7 || y < 0 || (x+directionX) > 7 || (x+directionX) < 0 || (y+directionY) > 7 || (y+directionY) < 0 ){ |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
const record = getPieceRecord(x, y); |
||||||
|
|
||||||
|
if (record) { |
||||||
|
if (record.player == player) { |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
const firstBlockRecord = getPieceRecord(x+directionX,y+directionY); |
||||||
|
const secondBlockRecord = getPieceRecord(x-directionX,y-directionY); |
||||||
|
|
||||||
|
|
||||||
|
return ((!firstBlockRecord || firstBlockRecord.id == pieceRecord.id) && (!secondBlockRecord || secondBlockRecord.id == pieceRecord.id)); |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
function continuable(pieceRecord){ |
||||||
|
const x = pieceRecord.x; |
||||||
|
const y = pieceRecord.y; |
||||||
|
const player = pieceRecord.player; |
||||||
|
const type = pieceRecord.type; |
||||||
|
|
||||||
|
if(player == "white" && ((getPieceRecord(x+1, y+1) && getPieceRecord(x+1, y+1).player != player && !getPieceRecord(x+2, y+2) && (y+2) < 8 && (x+2) < 8) || ((getPieceRecord(x-1, y+1) && getPieceRecord(x-1, y+1).player != player && !getPieceRecord(x-2, y+2) && (y+2) < 8 && (x-2) > 0)))){ |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
if( player == "black" && ((getPieceRecord(x+1, y-1) && getPieceRecord(x+1, y-1).player != player && !getPieceRecord(x+2, y-2) && (y-2) > 0 && (x+2) < 8) || ((getPieceRecord(x-1, y-1) && getPieceRecord(x-1, y-1).player != player && !getPieceRecord(x-2, y-2) && (y-2) > 0 && (x-2) > 0)))){ |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
if(type == "queen"){ |
||||||
|
|
||||||
|
return (continuableInDirectionForQueen(1,1,pieceRecord) || continuableInDirectionForQueen(-1,-1,pieceRecord) || continuableInDirectionForQueen(-1,1,pieceRecord) || continuableInDirectionForQueen(1,-1,pieceRecord)); |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function getOportunities(pieceRecord){ |
||||||
|
const player = pieceRecord.player; |
||||||
|
const opportunities = new Array(); |
||||||
|
|
||||||
|
pieceList.forEach(record => { |
||||||
|
if (record.player == player && record != pieceRecord && continuable(record)) { |
||||||
|
|
||||||
|
opportunities.push(record.id); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
return opportunities; |
||||||
|
} |
||||||
|
|
||||||
|
function getOportunitiesIncludingSelf(pieceRecord){ |
||||||
|
const player = pieceRecord.player; |
||||||
|
const opportunities = new Array(); |
||||||
|
|
||||||
|
pieceList.forEach(record => { |
||||||
|
if (record.player == player && continuable(record)) { |
||||||
|
|
||||||
|
opportunities.push(record.id); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
return opportunities; |
||||||
|
} |
||||||
|
|
||||||
|
function isMoveValid(pieceRecord, x, y){ |
||||||
|
|
||||||
|
|
||||||
|
if(0 > y > 7 || 0 > x > 7){ |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (getPieceRecord(x,y)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
const currentX = pieceRecord.x; |
||||||
|
const currentY = pieceRecord.y; |
||||||
|
|
||||||
|
if (Math.abs(y - currentY) != Math.abs(x - currentX)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
const player = pieceRecord.player; |
||||||
|
const type = pieceRecord.type; |
||||||
|
|
||||||
|
//Check correct Y direction if piece is basic
|
||||||
|
if (currentY > y && player == "white" && type == "basic") { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
const directionY = (y > currentY) ? 1 : -1; |
||||||
|
const directionX = (x > currentX) ? 1 : -1; |
||||||
|
|
||||||
|
space = 0; |
||||||
|
piecesInWay = 0; |
||||||
|
|
||||||
|
for (let i = 1; i < (Math.abs(y-currentY)+1); i++) { |
||||||
|
|
||||||
|
const nowX = currentX + (i*directionX); |
||||||
|
const nowY = currentY + (i*directionY); |
||||||
|
|
||||||
|
if (inWayPieceRecord = getPieceRecord(nowX, nowY)) { |
||||||
|
|
||||||
|
if(inWayPieceRecord.player == player){ |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
space = 0; |
||||||
|
piecesInWay++; |
||||||
|
}else{ |
||||||
|
space++; |
||||||
|
piecesInWay = 0; |
||||||
|
} |
||||||
|
|
||||||
|
if(piecesInWay > 1){ |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (space > 1 && type == "basic") { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
|
||||||
|
} |
||||||
|
function swapPlayers() {
|
||||||
|
currentPlayer = (currentPlayer == "white") ? "black" : "white"; |
||||||
|
} |
||||||
|
|
||||||
|
function makeMove(pieceRecord, x, y){ |
||||||
|
const player = pieceRecord.player; |
||||||
|
|
||||||
|
if (!isMoveValid(pieceRecord, x, y)) { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
const oldX = pieceRecord.x; |
||||||
|
const oldY = pieceRecord.y; |
||||||
|
|
||||||
|
const casualties = getCasualties(oldX, oldY, x, y); |
||||||
|
|
||||||
|
|
||||||
|
casualties.forEach(casualty => { |
||||||
|
nukePieceRecord(casualty); |
||||||
|
}); |
||||||
|
|
||||||
|
missedOportunities = new Array(); |
||||||
|
oportunities = getOportunitiesIncludingSelf(pieceRecord); |
||||||
|
|
||||||
|
if(casualties.length == 0 && oportunities.length != 0) { |
||||||
|
missedOportunities = oportunities; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
const original_type = pieceRecord.type; |
||||||
|
|
||||||
|
if ((player == "white" && y == 7) || (player == "black" && y == 0) && pieceRecord.type == "basic") { |
||||||
|
|
||||||
|
pieceRecord.type = "queen"; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
const updatedPieceRecord = updatePieceRecordPosition(pieceRecord, x, y); |
||||||
|
|
||||||
|
if (continuable(updatedPieceRecord) && casualties.length > 0 && pieceRecord.type == original_type) { |
||||||
|
return 2; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
return 1; |
||||||
|
} |
||||||
|
|
||||||
|
function nukePieceRecordThatMissedOportunity(pieceRecord) { |
||||||
|
if (missedOportunities.includes(pieceRecord.id)) { |
||||||
|
missedOportunities = new Array(); |
||||||
|
nukePieceRecord(pieceRecord); |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
generateDefaultPositions(); |
||||||
@ -0,0 +1,4 @@ |
|||||||
|
node_modules/ |
||||||
|
docs/ |
||||||
|
test/ |
||||||
|
dist/ |
||||||
@ -0,0 +1,280 @@ |
|||||||
|
module.exports = { |
||||||
|
"env": { |
||||||
|
"browser": true, |
||||||
|
"es6": true, |
||||||
|
"node": true |
||||||
|
}, |
||||||
|
"extends": "eslint:recommended", |
||||||
|
"globals": { |
||||||
|
"Atomics": "readonly", |
||||||
|
"SharedArrayBuffer": "readonly", |
||||||
|
"define": "writable", |
||||||
|
"chance": "writable" |
||||||
|
}, |
||||||
|
"parserOptions": { |
||||||
|
"ecmaVersion": 2018 |
||||||
|
}, |
||||||
|
"rules": { |
||||||
|
"accessor-pairs": "error", |
||||||
|
"array-bracket-newline": "off", |
||||||
|
"array-bracket-spacing": "off", |
||||||
|
"array-callback-return": "error", |
||||||
|
"array-element-newline": "off", |
||||||
|
"arrow-body-style": "error", |
||||||
|
"arrow-parens": "error", |
||||||
|
"arrow-spacing": "error", |
||||||
|
"block-scoped-var": "off", |
||||||
|
"block-spacing": "off", |
||||||
|
"brace-style": "off", |
||||||
|
"callback-return": "error", |
||||||
|
"camelcase": "off", |
||||||
|
"capitalized-comments": "off", |
||||||
|
"class-methods-use-this": "error", |
||||||
|
"comma-dangle": "off", |
||||||
|
"comma-spacing": "off", |
||||||
|
"comma-style": [ |
||||||
|
"error", |
||||||
|
"last" |
||||||
|
], |
||||||
|
"complexity": "off", |
||||||
|
"computed-property-spacing": [ |
||||||
|
"error", |
||||||
|
"never" |
||||||
|
], |
||||||
|
"consistent-return": "off", |
||||||
|
"consistent-this": "off", |
||||||
|
"curly": "error", |
||||||
|
"default-case": "off", |
||||||
|
"dot-location": [ |
||||||
|
"error", |
||||||
|
"property" |
||||||
|
], |
||||||
|
"dot-notation": "error", |
||||||
|
"eol-last": "error", |
||||||
|
"eqeqeq": "error", |
||||||
|
"func-call-spacing": "off", |
||||||
|
"func-name-matching": "error", |
||||||
|
"func-names": "off", |
||||||
|
"func-style": "off", |
||||||
|
"function-call-argument-newline": [ |
||||||
|
"error", |
||||||
|
"consistent" |
||||||
|
], |
||||||
|
"function-paren-newline": "error", |
||||||
|
"generator-star-spacing": "error", |
||||||
|
"global-require": "error", |
||||||
|
"guard-for-in": "off", |
||||||
|
"handle-callback-err": "error", |
||||||
|
"id-blacklist": "error", |
||||||
|
"id-length": "off", |
||||||
|
"id-match": "error", |
||||||
|
"implicit-arrow-linebreak": "error", |
||||||
|
"indent": "off", |
||||||
|
"indent-legacy": "off", |
||||||
|
"init-declarations": "off", |
||||||
|
"jsx-quotes": "error", |
||||||
|
"key-spacing": "off", |
||||||
|
"keyword-spacing": "off", |
||||||
|
"line-comment-position": "off", |
||||||
|
"linebreak-style": [ |
||||||
|
"error", |
||||||
|
"unix" |
||||||
|
], |
||||||
|
"lines-around-comment": "off", |
||||||
|
"lines-around-directive": "error", |
||||||
|
"lines-between-class-members": "error", |
||||||
|
"max-classes-per-file": "error", |
||||||
|
"max-depth": "error", |
||||||
|
"max-len": "off", |
||||||
|
"max-lines": "off", |
||||||
|
"max-lines-per-function": "off", |
||||||
|
"max-nested-callbacks": "error", |
||||||
|
"max-params": "off", |
||||||
|
"max-statements": "off", |
||||||
|
"max-statements-per-line": "off", |
||||||
|
"multiline-comment-style": "off", |
||||||
|
"multiline-ternary": [ |
||||||
|
"error", |
||||||
|
"always-multiline" |
||||||
|
], |
||||||
|
"new-cap": "error", |
||||||
|
"new-parens": "error", |
||||||
|
"newline-after-var": "off", |
||||||
|
"newline-before-return": "off", |
||||||
|
"newline-per-chained-call": "off", |
||||||
|
"no-alert": "error", |
||||||
|
"no-array-constructor": "off", |
||||||
|
"no-await-in-loop": "error", |
||||||
|
"no-bitwise": "off", |
||||||
|
"no-buffer-constructor": "off", |
||||||
|
"no-caller": "error", |
||||||
|
"no-catch-shadow": "error", |
||||||
|
"no-confusing-arrow": "error", |
||||||
|
"no-console": "error", |
||||||
|
"no-continue": "error", |
||||||
|
"no-div-regex": "error", |
||||||
|
"no-duplicate-imports": "error", |
||||||
|
"no-else-return": "off", |
||||||
|
"no-empty-function": "off", |
||||||
|
"no-eq-null": "error", |
||||||
|
"no-eval": "error", |
||||||
|
"no-extend-native": "error", |
||||||
|
"no-extra-bind": "error", |
||||||
|
"no-extra-boolean-cast": "off", |
||||||
|
"no-extra-label": "error", |
||||||
|
"no-extra-parens": "off", |
||||||
|
"no-floating-decimal": "error", |
||||||
|
"no-implicit-coercion": [ |
||||||
|
"error", |
||||||
|
{ |
||||||
|
"boolean": false, |
||||||
|
"number": false, |
||||||
|
"string": false |
||||||
|
} |
||||||
|
], |
||||||
|
"no-implicit-globals": "off", |
||||||
|
"no-implied-eval": "error", |
||||||
|
"no-inline-comments": "off", |
||||||
|
"no-inner-declarations": [ |
||||||
|
"error", |
||||||
|
"functions" |
||||||
|
], |
||||||
|
"no-invalid-this": "error", |
||||||
|
"no-iterator": "error", |
||||||
|
"no-label-var": "error", |
||||||
|
"no-labels": "error", |
||||||
|
"no-lone-blocks": "error", |
||||||
|
"no-lonely-if": "off", |
||||||
|
"no-loop-func": "error", |
||||||
|
"no-magic-numbers": "off", |
||||||
|
"no-mixed-operators": "off", |
||||||
|
"no-mixed-requires": "error", |
||||||
|
"no-multi-assign": "off", |
||||||
|
"no-multi-spaces": "off", |
||||||
|
"no-multi-str": "error", |
||||||
|
"no-multiple-empty-lines": "error", |
||||||
|
"no-native-reassign": "error", |
||||||
|
"no-negated-condition": "off", |
||||||
|
"no-negated-in-lhs": "error", |
||||||
|
"no-nested-ternary": "off", |
||||||
|
"no-new": "error", |
||||||
|
"no-new-func": "error", |
||||||
|
"no-new-object": "error", |
||||||
|
"no-new-require": "error", |
||||||
|
"no-new-wrappers": "error", |
||||||
|
"no-octal-escape": "error", |
||||||
|
"no-param-reassign": "off", |
||||||
|
"no-path-concat": "error", |
||||||
|
"no-plusplus": "off", |
||||||
|
"no-process-env": "error", |
||||||
|
"no-process-exit": "error", |
||||||
|
"no-proto": "error", |
||||||
|
"no-restricted-globals": "error", |
||||||
|
"no-restricted-imports": "error", |
||||||
|
"no-restricted-modules": "error", |
||||||
|
"no-restricted-properties": "error", |
||||||
|
"no-restricted-syntax": "error", |
||||||
|
"no-return-assign": "error", |
||||||
|
"no-return-await": "error", |
||||||
|
"no-script-url": "error", |
||||||
|
"no-self-compare": "error", |
||||||
|
"no-sequences": "error", |
||||||
|
"no-shadow": "off", |
||||||
|
"no-spaced-func": "off", |
||||||
|
"no-sync": "error", |
||||||
|
"no-tabs": [ |
||||||
|
"error", |
||||||
|
{ |
||||||
|
"allowIndentationTabs": true |
||||||
|
} |
||||||
|
], |
||||||
|
"no-template-curly-in-string": "error", |
||||||
|
"no-ternary": "off", |
||||||
|
"no-throw-literal": "error", |
||||||
|
"no-trailing-spaces": "error", |
||||||
|
"no-undef-init": "error", |
||||||
|
"no-undefined": "off", |
||||||
|
"no-underscore-dangle": "off", |
||||||
|
"no-unmodified-loop-condition": "error", |
||||||
|
"no-unneeded-ternary": "error", |
||||||
|
"no-unused-expressions": "error", |
||||||
|
"no-use-before-define": "off", |
||||||
|
"no-useless-call": "error", |
||||||
|
"no-useless-computed-key": "error", |
||||||
|
"no-useless-concat": "error", |
||||||
|
"no-useless-constructor": "error", |
||||||
|
"no-useless-rename": "error", |
||||||
|
"no-useless-return": "error", |
||||||
|
"no-var": "off", |
||||||
|
"no-void": "error", |
||||||
|
"no-warning-comments": "off", |
||||||
|
"no-whitespace-before-property": "error", |
||||||
|
"nonblock-statement-body-position": "error", |
||||||
|
"object-curly-newline": "error", |
||||||
|
"object-curly-spacing": "off", |
||||||
|
"object-shorthand": "off", |
||||||
|
"one-var": "off", |
||||||
|
"one-var-declaration-per-line": "off", |
||||||
|
"operator-assignment": "off", |
||||||
|
"operator-linebreak": [ |
||||||
|
"error", |
||||||
|
"after" |
||||||
|
], |
||||||
|
"padded-blocks": "off", |
||||||
|
"padding-line-between-statements": "error", |
||||||
|
"prefer-arrow-callback": "off", |
||||||
|
"prefer-const": "error", |
||||||
|
"prefer-destructuring": "off", |
||||||
|
"prefer-named-capture-group": "error", |
||||||
|
"prefer-numeric-literals": "error", |
||||||
|
"prefer-object-spread": "error", |
||||||
|
"prefer-promise-reject-errors": "error", |
||||||
|
"prefer-reflect": "off", |
||||||
|
"prefer-rest-params": "off", |
||||||
|
"prefer-spread": "off", |
||||||
|
"prefer-template": "off", |
||||||
|
"quote-props": "off", |
||||||
|
"quotes": "off", |
||||||
|
"radix": "off", |
||||||
|
"require-await": "error", |
||||||
|
"require-jsdoc": "off", |
||||||
|
"require-unicode-regexp": "off", |
||||||
|
"rest-spread-spacing": "error", |
||||||
|
"semi": "off", |
||||||
|
"semi-spacing": "off", |
||||||
|
"semi-style": "off", |
||||||
|
"sort-imports": "error", |
||||||
|
"sort-keys": "off", |
||||||
|
"sort-vars": "off", |
||||||
|
"space-before-blocks": "off", |
||||||
|
"space-before-function-paren": "off", |
||||||
|
"space-in-parens": "off", |
||||||
|
"space-infix-ops": "off", |
||||||
|
"space-unary-ops": "error", |
||||||
|
"spaced-comment": "off", |
||||||
|
"strict": [ |
||||||
|
"error", |
||||||
|
"never" |
||||||
|
], |
||||||
|
"switch-colon-spacing": "error", |
||||||
|
"symbol-description": "error", |
||||||
|
"template-curly-spacing": "error", |
||||||
|
"template-tag-spacing": "error", |
||||||
|
"unicode-bom": [ |
||||||
|
"error", |
||||||
|
"never" |
||||||
|
], |
||||||
|
"valid-jsdoc": "off", |
||||||
|
"vars-on-top": "off", |
||||||
|
"wrap-iife": [ |
||||||
|
"error", |
||||||
|
"any" |
||||||
|
], |
||||||
|
"wrap-regex": "off", |
||||||
|
"yield-star-spacing": "error", |
||||||
|
"yoda": [ |
||||||
|
"error", |
||||||
|
"never" |
||||||
|
] |
||||||
|
} |
||||||
|
}; |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
# ✨ Chance.js Pull Request 💻 |
||||||
|
|
||||||
|
 |
||||||
|
|
||||||
|
## Pull Request details |
||||||
|
|
||||||
|
- INFO ABOUT YOUR PULL REQUEST GOES HERE (Please be as descriptive as possible) 🤜 |
||||||
|
- Use bullet points to make it easy to read. |
||||||
|
|
||||||
|
## Any Breaking changes |
||||||
|
|
||||||
|
- IF ANYTHING YOU'RE COMMITTING WOULD BREAK SOMETHING, INCLUDE HERE WHAT WOULD BREAK |
||||||
|
- IF YOU HAVE NO BREAKING CHANGES, ENTER 'None' |
||||||
|
|
||||||
|
## Example Functionality for new functions (Only for NEW feature addition/enhancements) |
||||||
|
|
||||||
|
- INCLUDE CODE SNIPPETS AND EXCEPTED OUTPUT HERE. |
||||||
|
- IF YOU HAVE NO NEW FUNCTIONALITY, ENTER 'None' |
||||||
|
|
||||||
|
|
||||||
|
## Associated Screenshots |
||||||
|
|
||||||
|
- IF YOU HAVE ANY SCREENSHOTS, INCLUDE THEM HERE. _( Welcome file extensions include gifs/png screenshots of your feature in action )_ |
||||||
|
- IF YOU HAVE NO SCREENSHOTS, ENTER 'None' |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
name: Code Coverage |
||||||
|
|
||||||
|
on: [push, pull_request] |
||||||
|
|
||||||
|
jobs: |
||||||
|
build: |
||||||
|
runs-on: ubuntu-latest |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v1 |
||||||
|
- name: Use Node.js 16.x |
||||||
|
uses: actions/setup-node@v1 |
||||||
|
with: |
||||||
|
node-version: 16.x |
||||||
|
- name: install and coverage |
||||||
|
run: | |
||||||
|
yarn |
||||||
|
yarn coverage |
||||||
|
env: |
||||||
|
CI: true |
||||||
|
|
||||||
|
- name: Coveralls |
||||||
|
uses: coverallsapp/github-action@master |
||||||
|
with: |
||||||
|
github-token: ${{ secrets.github_token }} |
||||||
@ -0,0 +1,34 @@ |
|||||||
|
name: Documentation |
||||||
|
|
||||||
|
on: |
||||||
|
pull_request: |
||||||
|
branches: |
||||||
|
- master |
||||||
|
paths: |
||||||
|
- 'docs/**/*' |
||||||
|
push: |
||||||
|
branches: |
||||||
|
- master |
||||||
|
paths: |
||||||
|
- 'docs/**/*' |
||||||
|
|
||||||
|
jobs: |
||||||
|
build: |
||||||
|
runs-on: ubuntu-latest |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v1 |
||||||
|
- uses: actions/setup-node@v1 |
||||||
|
with: |
||||||
|
node-version: 16 |
||||||
|
- name: build and deploy |
||||||
|
if: github.event_name == 'pull_request' && github.event.pull_request.merged == 'true' |
||||||
|
run: | |
||||||
|
yarn |
||||||
|
yarn docs |
||||||
|
yarn docs:publish |
||||||
|
env: |
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
||||||
|
GITHUB_REPO: chancejs/chancejs |
||||||
|
GIT_NAME: Travis CI |
||||||
|
GIT_EMAIL: mail@victorquinn.ocm |
||||||
|
GIT_SOURCE: _docpress |
||||||
@ -0,0 +1,18 @@ |
|||||||
|
name: Lint |
||||||
|
|
||||||
|
on: [push, pull_request] |
||||||
|
|
||||||
|
jobs: |
||||||
|
eslint: |
||||||
|
runs-on: ubuntu-latest |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v1 |
||||||
|
with: |
||||||
|
fetch-depth: 1 |
||||||
|
- uses: actions/setup-node@v1 |
||||||
|
with: |
||||||
|
node-version: 16 |
||||||
|
- name: eslint |
||||||
|
uses: reviewdog/action-eslint@v1 |
||||||
|
with: |
||||||
|
reporter: github-pr-review |
||||||
@ -0,0 +1,53 @@ |
|||||||
|
name: NPM Publish Package |
||||||
|
|
||||||
|
on: |
||||||
|
pull_request: |
||||||
|
branches: |
||||||
|
- master |
||||||
|
push: |
||||||
|
branches: |
||||||
|
- master |
||||||
|
|
||||||
|
jobs: |
||||||
|
build: |
||||||
|
runs-on: ubuntu-latest |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v1 |
||||||
|
- uses: actions/setup-node@v1 |
||||||
|
with: |
||||||
|
node-version: 16 |
||||||
|
- name: install and test |
||||||
|
run: | |
||||||
|
yarn |
||||||
|
yarn run test |
||||||
|
|
||||||
|
publish-npm: |
||||||
|
needs: build |
||||||
|
runs-on: ubuntu-latest |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v1 |
||||||
|
- uses: actions/setup-node@v1 |
||||||
|
with: |
||||||
|
node-version: 16 |
||||||
|
registry-url: https://registry.npmjs.org/ |
||||||
|
- name: Publish to NPM |
||||||
|
if: github.event_name == 'pull_request' && github.event.pull_request.merged == 'true' |
||||||
|
run: npm publish |
||||||
|
env: |
||||||
|
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
||||||
|
|
||||||
|
# Commented out until we have access to the Github Package Registry |
||||||
|
# |
||||||
|
# publish-gpr: |
||||||
|
# needs: build |
||||||
|
# runs-on: ubuntu-latest |
||||||
|
# steps: |
||||||
|
# - uses: actions/checkout@v1 |
||||||
|
# - uses: actions/setup-node@v1 |
||||||
|
# with: |
||||||
|
# node-version: 12 |
||||||
|
# registry-url: https://npm.pkg.github.com/ |
||||||
|
# scope: '@victorquinn' |
||||||
|
# - run: npm publish |
||||||
|
# env: |
||||||
|
# NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
||||||
@ -0,0 +1,23 @@ |
|||||||
|
name: Unit Tests |
||||||
|
|
||||||
|
on: [push, pull_request] |
||||||
|
|
||||||
|
jobs: |
||||||
|
build: |
||||||
|
runs-on: ubuntu-latest |
||||||
|
strategy: |
||||||
|
matrix: |
||||||
|
node-version: [12.x, 16.x, 18.x] |
||||||
|
|
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v1 |
||||||
|
- name: Use Node.js ${{ matrix.node-version }} |
||||||
|
uses: actions/setup-node@v1 |
||||||
|
with: |
||||||
|
node-version: ${{ matrix.node-version }} |
||||||
|
- name: yarn install and test |
||||||
|
run: | |
||||||
|
yarn |
||||||
|
yarn run test |
||||||
|
env: |
||||||
|
CI: true |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
The MIT License (MIT) |
||||||
|
|
||||||
|
Copyright (c) 2015 Victor Quinn |
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||||
|
of this software and associated documentation files (the "Software"), to deal |
||||||
|
in the Software without restriction, including without limitation the rights |
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||||
|
copies of the Software, and to permit persons to whom the Software is |
||||||
|
furnished to do so, subject to the following conditions: |
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all |
||||||
|
copies or substantial portions of the Software. |
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||||
|
SOFTWARE. |
||||||
@ -0,0 +1,61 @@ |
|||||||
|
# Chance |
||||||
|
|
||||||
|
[](http://chancejs.com) |
||||||
|
|
||||||
|
[](https://travis-ci.org/chancejs/chancejs) [](https://github.com/chancejs/chancejs) [](https://github.com/chancejs/chancejs) [](https://npmjs.com/package/chance) [](https://www.jsdelivr.com/package/npm/chance) [](https://npmjs.com/package/chance) [](https://coveralls.io/r/chancejs/chancejs?branch=master) [](https://github.com/chancejs/chancejs) |
||||||
|
|
||||||
|
Chance - Random generator helper for JavaScript |
||||||
|
|
||||||
|
Homepage: [http://chancejs.com](http://chancejs.com) |
||||||
|
|
||||||
|
Many more details on [http://chancejs.com](http://chancejs.com) but this single |
||||||
|
library can generate random numbers, characters, strings, names, addresses, |
||||||
|
dice, and pretty much anything else. |
||||||
|
|
||||||
|
It includes the basic building blocks for all these items and is built on top |
||||||
|
of a Mersenne Twister so it can generate these things with repeatability, if |
||||||
|
desired. |
||||||
|
|
||||||
|
## Usage |
||||||
|
|
||||||
|
See the [full docs](https://chancejs.com) for details on installation and usage. |
||||||
|
|
||||||
|
## Dependent tools |
||||||
|
|
||||||
|
* [Chance CLI](https://github.com/chancejs/chance-cli) - Use Chance on the command line. |
||||||
|
* [Chance Token Replacer](https://github.com/drewbrokke/chance-token-replacer) - Replace tokens in a string with Chance generated items. |
||||||
|
* [Dream.js](https://github.com/adleroliveira/dreamjs) - Lightweight json data generator |
||||||
|
* [Fake JSON Schema](https://github.com/pateketrueke/json-schema-faker/) - Use chance generators to populate JSON Schema samples. |
||||||
|
* [Mocker Data Generator](https://github.com/danibram/mocker-data-generator/) - Minimal JSON data generator. |
||||||
|
* [swagger-mock-api](https://github.com/dzdrazil/swagger-mock-api/) - Generate API mocks from a Swagger spec file enriched with Chance types and constraints |
||||||
|
* [fony](https://github.com/captainsafia/fony) - A simple command line tool for generating fake data from a template string |
||||||
|
|
||||||
|
Or [view all of the dependents on npm](https://www.npmjs.com/package/chance) |
||||||
|
|
||||||
|
*Know a library that uses Chance that isn't here? Update the README and submit a PR!* |
||||||
|
|
||||||
|
## Author |
||||||
|
### Victor Quinn |
||||||
|
[https://www.victorquinn.com](https://www.victorquinn.com) |
||||||
|
[@victorquinn](https://twitter.com/victorquinn) |
||||||
|
|
||||||
|
Please feel free to reach out to me if you have any questions or suggestions. |
||||||
|
|
||||||
|
### Contributors |
||||||
|
|
||||||
|
THANK YOU! |
||||||
|
|
||||||
|
### Contribute! |
||||||
|
|
||||||
|
Be a part of this project! You can run the test using the following. |
||||||
|
|
||||||
|
**Note: Make sure you have Yarn installed globally** |
||||||
|
|
||||||
|
1. Install dependencies from package.json by running ```yarn``` |
||||||
|
2. Run the test suite via ```yarn test``` |
||||||
|
3. Make some fun new modules! |
||||||
|
|
||||||
|
This project is licensed under the [MIT License](http://en.wikipedia.org/wiki/MIT_License) so feel free to hack away :) |
||||||
|
|
||||||
|
Proudly written in Washington, D.C. |
||||||
|
|
||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1 @@ |
|||||||
|
chancejs.com |
||||||
@ -0,0 +1,127 @@ |
|||||||
|
* [Chance](intro.md) |
||||||
|
* Usage |
||||||
|
* [bower](usage/bower.md) |
||||||
|
* [browser](usage/browser.md) |
||||||
|
* [cli](usage/cli.md) |
||||||
|
* [node](usage/node.md) |
||||||
|
* [requirejs](usage/requirejs.md) |
||||||
|
* [seed](usage/seed.md) |
||||||
|
* [function](usage/function.md) |
||||||
|
* Basics |
||||||
|
* [bool](basics/bool.md) |
||||||
|
* [falsy](basics/falsy.md) |
||||||
|
* [character](basics/character.md) |
||||||
|
* [floating](basics/floating.md) |
||||||
|
* [integer](basics/integer.md) |
||||||
|
* [letter](basics/letter.md) |
||||||
|
* [natural](basics/natural.md) |
||||||
|
* [prime](basics/prime.md) |
||||||
|
* [string](basics/string.md) |
||||||
|
* [template](basics/template.md) |
||||||
|
* Text |
||||||
|
* [paragraph](text/paragraph.md) |
||||||
|
* [sentence](text/sentence.md) |
||||||
|
* [syllable](text/syllable.md) |
||||||
|
* [word](text/word.md) |
||||||
|
* Person |
||||||
|
* [age](person/age.md) |
||||||
|
* [birthday](person/birthday.md) |
||||||
|
* [cf](person/cf.md) |
||||||
|
* [cpf](person/cpf.md) |
||||||
|
* [first](person/first.md) |
||||||
|
* [gender](person/gender.md) |
||||||
|
* [last](person/last.md) |
||||||
|
* [name](person/name.md) |
||||||
|
* [prefix](person/prefix.md) |
||||||
|
* [ssn](person/ssn.md) |
||||||
|
* [suffix](person/suffix.md) |
||||||
|
* Thing |
||||||
|
* [animal](thing/animal.md) |
||||||
|
* Mobile |
||||||
|
* [android_id](mobile/android_id.md) |
||||||
|
* [apple_token](mobile/apple_token.md) |
||||||
|
* [bb_pin](mobile/bb_pin.md) |
||||||
|
* [wp7_anid](mobile/wp7_anid.md) |
||||||
|
* [wp8_anid2](mobile/wp8_anid2.md) |
||||||
|
* Web |
||||||
|
* [avatar](web/avatar.md) |
||||||
|
* [color](web/color.md) |
||||||
|
* [company](web/company.md) |
||||||
|
* [domain](web/domain.md) |
||||||
|
* [email](web/email.md) |
||||||
|
* [fbid](web/fbid.md) |
||||||
|
* [google_analytics](web/google_analytics.md) |
||||||
|
* [hashtag](web/hashtag.md) |
||||||
|
* [ip](web/ip.md) |
||||||
|
* [ipv6](web/ipv6.md) |
||||||
|
* [klout](web/klout.md) |
||||||
|
* [profession](web/profession.md) |
||||||
|
* [tld](web/tld.md) |
||||||
|
* [twitter](web/twitter.md) |
||||||
|
* [url](web/url.md) |
||||||
|
* Location |
||||||
|
* [address](location/address.md) |
||||||
|
* [altitude](location/altitude.md) |
||||||
|
* [areacode](location/areacode.md) |
||||||
|
* [city](location/city.md) |
||||||
|
* [coordinates](location/coordinates.md) |
||||||
|
* [country](location/country.md) |
||||||
|
* [depth](location/depth.md) |
||||||
|
* [geohash](location/geohash.md) |
||||||
|
* [latitude](location/latitude.md) |
||||||
|
* [locale](location/locale.md) |
||||||
|
* [longitude](location/longitude.md) |
||||||
|
* [phone](location/phone.md) |
||||||
|
* [postal](location/postal.md) |
||||||
|
* [postcode](location/postcode.md) |
||||||
|
* [province](location/province.md) |
||||||
|
* [state](location/state.md) |
||||||
|
* [street](location/street.md) |
||||||
|
* [zip](location/zip.md) |
||||||
|
* Time |
||||||
|
* [ampm](time/ampm.md) |
||||||
|
* [date](time/date.md) |
||||||
|
* [hammertime](time/hammertime.md) |
||||||
|
* [hour](time/hour.md) |
||||||
|
* [millisecond](time/millisecond.md) |
||||||
|
* [minute](time/minute.md) |
||||||
|
* [month](time/month.md) |
||||||
|
* [second](time/second.md) |
||||||
|
* [timestamp](time/timestamp.md) |
||||||
|
* [timezone](time/timezone.md) |
||||||
|
* [weekday](time/weekday.md) |
||||||
|
* [year](time/year.md) |
||||||
|
* Finance |
||||||
|
* [cc](finance/cc.md) |
||||||
|
* [cc_type](finance/cc_type.md) |
||||||
|
* [currency](finance/currency.md) |
||||||
|
* [currency_pair](finance/currency_pair.md) |
||||||
|
* [dollar](finance/dollar.md) |
||||||
|
* [euro](finance/euro.md) |
||||||
|
* [exp](finance/exp.md) |
||||||
|
* [exp_month](finance/exp_month.md) |
||||||
|
* [exp_year](finance/exp_year.md) |
||||||
|
* Helpers |
||||||
|
* [capitalize](helpers/capitalize.md) |
||||||
|
* [mixin](helpers/mixin.md) |
||||||
|
* [pad](helpers/pad.md) |
||||||
|
* [pick](helpers/pick.md) |
||||||
|
* [pickone](helpers/pickone.md) |
||||||
|
* [pickset](helpers/pickset.md) |
||||||
|
* [set](helpers/set.md) |
||||||
|
* [shuffle](helpers/shuffle.md) |
||||||
|
* Miscellaneous |
||||||
|
* [coin](miscellaneous/coin.md) |
||||||
|
* [dice](miscellaneous/dice.md) |
||||||
|
* [guid](miscellaneous/guid.md) |
||||||
|
* [hash](miscellaneous/hash.md) |
||||||
|
* [hidden](miscellaneous/hidden.md) |
||||||
|
* [n](miscellaneous/n.md) |
||||||
|
* [normal](miscellaneous/normal.md) |
||||||
|
* [radio](miscellaneous/radio.md) |
||||||
|
* [rpg](miscellaneous/rpg.md) |
||||||
|
* [tv](miscellaneous/tv.md) |
||||||
|
* [unique](miscellaneous/unique.md) |
||||||
|
* [weighted](miscellaneous/weighted.md) |
||||||
|
|
||||||
|
<script type="text/javascript" src="chance.js"></script> |
||||||
@ -0,0 +1,3 @@ |
|||||||
|
window.analytics=window.analytics||[],window.analytics.methods=["identify","group","track","page","pageview","alias","ready","on","once","off","trackLink","trackForm","trackClick","trackSubmit"],window.analytics.factory=function(t){return function(){var a=Array.prototype.slice.call(arguments);return a.unshift(t),window.analytics.push(a),window.analytics}};for(var i=0;i<window.analytics.methods.length;i++){var key=window.analytics.methods[i];window.analytics[key]=window.analytics.factory(key)}window.analytics.load=function(t){if(!document.getElementById("analytics-js")){var a=document.createElement("script");a.type="text/javascript",a.id="analytics-js",a.async=!0,a.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.io/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(a,n)}},window.analytics.SNIPPET_VERSION="2.0.9", |
||||||
|
window.analytics.load("5u8961mhro"); |
||||||
|
window.analytics.page(); |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
# bool |
||||||
|
|
||||||
|
```js |
||||||
|
// usages |
||||||
|
chance.bool() |
||||||
|
chance.bool({ likelihood: 30 }) |
||||||
|
``` |
||||||
|
Return a random boolean value (`true` or `false`). |
||||||
|
|
||||||
|
```js |
||||||
|
chance.bool(); |
||||||
|
=> true |
||||||
|
``` |
||||||
|
|
||||||
|
The default likelihood of success (returning `true`) is 50%. |
||||||
|
Can optionally specify the likelihood in percent: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.bool({likelihood: 30}); |
||||||
|
=> false |
||||||
|
``` |
||||||
|
|
||||||
|
In this case only a 30% likelihood of `true`, and a 70% likelihood of `false`. |
||||||
|
|
||||||
@ -0,0 +1,65 @@ |
|||||||
|
# character |
||||||
|
|
||||||
|
```js |
||||||
|
// usages |
||||||
|
chance.character() |
||||||
|
chance.character({ pool: 'abcde' }) |
||||||
|
chance.character({ alpha: true }) |
||||||
|
chance.character({ numeric: true }) |
||||||
|
chance.character({ casing: 'lower' }) |
||||||
|
chance.character({ symbols: true }) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random character. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.character(); |
||||||
|
=> 'v' |
||||||
|
``` |
||||||
|
|
||||||
|
By default it will return a string with random character from the following |
||||||
|
pool. |
||||||
|
|
||||||
|
```js |
||||||
|
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify a pool and the character will be generated with characters |
||||||
|
only from that pool. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.character({ pool: 'abcde' }); |
||||||
|
=> 'c' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify alpha for an alphabetic character. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.character({ alpha: true }); |
||||||
|
=> 'N' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify numeric for a numeric character. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.character({ numeric: true }); |
||||||
|
=> '8' |
||||||
|
``` |
||||||
|
|
||||||
|
Default includes both upper and lower case. It's possible to specify one or the |
||||||
|
other. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.character({ casing: 'lower' }); |
||||||
|
=> 'j' |
||||||
|
``` |
||||||
|
|
||||||
|
*Note, wanted to call this key just ```case``` but unfortunately that's a |
||||||
|
reserved word in JavaScript for use in a switch statement* |
||||||
|
|
||||||
|
Optionally return only symbols |
||||||
|
|
||||||
|
```js |
||||||
|
chance.character({ symbols: true }); |
||||||
|
=> '%' |
||||||
|
``` |
||||||
@ -0,0 +1,22 @@ |
|||||||
|
# falsy |
||||||
|
|
||||||
|
```js |
||||||
|
// usages |
||||||
|
chance.falsy() |
||||||
|
chance.falsy({ pool: [ NaN, undefined ] }) |
||||||
|
``` |
||||||
|
Return a random falsy value (`false`, `null`, `undefined`, `0`, `NaN`, `''`). |
||||||
|
|
||||||
|
```js |
||||||
|
chance.falsy(); |
||||||
|
=> false |
||||||
|
``` |
||||||
|
|
||||||
|
The default pool can be changed to better meet the needs: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.falsy({ pool: [ NaN, undefined ] }); |
||||||
|
=> NaN |
||||||
|
``` |
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,44 @@ |
|||||||
|
# floating |
||||||
|
|
||||||
|
```js |
||||||
|
// usages |
||||||
|
chance.floating() |
||||||
|
chance.floating({ fixed: 7 }) |
||||||
|
chance.floating({ min: 0, max: 100 }) |
||||||
|
``` |
||||||
|
<p class="pullquote">I wanted to use float or double as the method name but both are JS reserved words even though they aren't really used...</p> |
||||||
|
|
||||||
|
Return a random floating point number. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.floating(); |
||||||
|
=> -211920142886.5024 |
||||||
|
``` |
||||||
|
|
||||||
|
By default it will return a fixed number of at most 4 digits after the decimal. |
||||||
|
|
||||||
|
Note: *at most* 4 digits. This because, unless we returned trailing zeroes |
||||||
|
(which aren't allowed on the JavaScript float) we can't guarantee 4 digits after |
||||||
|
the decimal. So if random chance comes back with `82383854.2000` then |
||||||
|
`82383854.2` is what will be returned. |
||||||
|
|
||||||
|
To retrieve a set number of fixed digits after the decimal, provide it as an option. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.floating({ fixed: 7 }); |
||||||
|
=> -749512327.7447168 |
||||||
|
``` |
||||||
|
|
||||||
|
As with other number functions, can include a min and/or max. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.floating({ min: 0, max: 100 }); |
||||||
|
=> 31.9021 |
||||||
|
``` |
||||||
|
|
||||||
|
Or combine them. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.floating({ min: 0, max: 100, fixed: 8 }); |
||||||
|
=> 45.92367599 |
||||||
|
``` |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
# integer |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.integer() |
||||||
|
chance.integer({ min: -20, max: 20 }) |
||||||
|
``` |
||||||
|
|
||||||
|
<p class="pullquote">9007199254740991 is 2^53 - 1 and is the largest number value in JavaScript</p> |
||||||
|
|
||||||
|
Return a random integer. |
||||||
|
|
||||||
|
_range: -9007199254740991 to 9007199254740991_ |
||||||
|
|
||||||
|
See: [Largest number in JavaScript](http://vq.io/16qnIYj) |
||||||
|
|
||||||
|
```js |
||||||
|
chance.integer(); |
||||||
|
=> -1293235 |
||||||
|
``` |
||||||
|
|
||||||
|
Can optionally provide min and max. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.integer({ min: -20, max: 20 }) |
||||||
|
=> -7 |
||||||
|
``` |
||||||
|
|
||||||
|
|
||||||
|
These min and max are inclusive, so they are included in the range. This means |
||||||
|
```js |
||||||
|
chance.integer({ min: -2, max: 2 }) |
||||||
|
``` |
||||||
|
would return either -2, -1, 0, 1, or 2. |
||||||
|
|
||||||
|
```js |
||||||
|
// Specific case |
||||||
|
-2 <= random number <= 2 |
||||||
|
|
||||||
|
// General case |
||||||
|
min <= random number <= max |
||||||
|
``` |
||||||
@ -0,0 +1,25 @@ |
|||||||
|
# letter |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.letter() |
||||||
|
chance.letter({ casing: 'lower' }) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random letter. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.letter(); |
||||||
|
=> 'p' |
||||||
|
``` |
||||||
|
|
||||||
|
By default it will return a random lowercase letter. |
||||||
|
|
||||||
|
<p class="pullquote">Note, wanted to call this option just <strong>case</strong> instead of <strong>casing</strong> but unfortunately that's a reserved word in JavaScript for use in a switch statement</p> |
||||||
|
|
||||||
|
It's possible to specify upper case |
||||||
|
|
||||||
|
```js |
||||||
|
chance.letter({casing: 'upper'}); |
||||||
|
=> 'A' |
||||||
|
``` |
||||||
@ -0,0 +1,47 @@ |
|||||||
|
# natural |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.natural() |
||||||
|
chance.natural({ min: 1, max: 20 }) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a natural number. |
||||||
|
|
||||||
|
_range: 0 to 9007199254740991_ |
||||||
|
|
||||||
|
```js |
||||||
|
chance.natural(); |
||||||
|
=> 125019392395 |
||||||
|
``` |
||||||
|
|
||||||
|
Can optionally provide min and max. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.natural({min: 1, max: 20}); |
||||||
|
=> 14 |
||||||
|
``` |
||||||
|
|
||||||
|
Can optionally provide numbers you wish to exclude. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.natural({min: 1, max: 5, exclude: [1, 3]}); |
||||||
|
=> 2 |
||||||
|
``` |
||||||
|
|
||||||
|
These are inclusive, so they are included in the range. This means |
||||||
|
```chance.natural({min: 1, max: 3});``` would return either 1, 2, or 3 or: |
||||||
|
|
||||||
|
```js |
||||||
|
// Specific case |
||||||
|
1 <= random number <= 3 |
||||||
|
|
||||||
|
// General case |
||||||
|
min <= random number <= max |
||||||
|
``` |
||||||
|
|
||||||
|
|
||||||
|
[Natural Number on Wikipedia][natural] |
||||||
|
|
||||||
|
[natural]: https://en.wikipedia.org/wiki/Natural_number |
||||||
|
|
||||||
@ -0,0 +1,41 @@ |
|||||||
|
# prime |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.prime() |
||||||
|
chance.prime({ min: 1, max: 20 }) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a prime number. |
||||||
|
|
||||||
|
_default range: 0 to 10000_ |
||||||
|
|
||||||
|
```js |
||||||
|
chance.prime(); |
||||||
|
=> 929 |
||||||
|
``` |
||||||
|
|
||||||
|
Can optionally provide min and max. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.prime({min: 1, max: 20}); |
||||||
|
=> 13 |
||||||
|
``` |
||||||
|
|
||||||
|
|
||||||
|
These are inclusive, so they are included in the range. This means |
||||||
|
```chance.prime({min: 2, max: 5});``` would return either 2, 3, or 5 or: |
||||||
|
|
||||||
|
```js |
||||||
|
// Specific case |
||||||
|
2 <= random number <= 5 |
||||||
|
|
||||||
|
// General case |
||||||
|
min <= random number <= max |
||||||
|
``` |
||||||
|
|
||||||
|
|
||||||
|
[Prime Number on Wikipedia][prime] |
||||||
|
|
||||||
|
[prime]: https://en.wikipedia.org/wiki/Prime_number |
||||||
|
|
||||||
@ -0,0 +1,77 @@ |
|||||||
|
# string |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.string() |
||||||
|
chance.string({ length: 5 }) |
||||||
|
chance.string({ min: 5 }) |
||||||
|
chance.string({ max: 50 }) |
||||||
|
chance.string({ min: 5, max: 20 }) |
||||||
|
chance.string({ pool: 'abcde' }) |
||||||
|
chance.string({ alpha: true }) |
||||||
|
chance.string({ numeric: true }) |
||||||
|
chance.string({ casing: 'lower' }) |
||||||
|
chance.string({ symbols: true }) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random string. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.string(); |
||||||
|
=> 'Z&Q78&fqkPq' |
||||||
|
``` |
||||||
|
|
||||||
|
By default it will return a string with random length of 5-20 characters and |
||||||
|
will contain any of the following characters. |
||||||
|
|
||||||
|
```js |
||||||
|
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()[]' |
||||||
|
``` |
||||||
|
|
||||||
|
Can optionally specify a length and the string will be exactly that length. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.string({ length: 5 }); |
||||||
|
=> 'YN%fG' |
||||||
|
``` |
||||||
|
|
||||||
|
Can optionally specify a min and the string will have a minimum length |
||||||
|
|
||||||
|
```js |
||||||
|
chance.string({ min: 5 }); |
||||||
|
=> '6(Ow1wF)qjUm%W)B2[Q]' |
||||||
|
``` |
||||||
|
|
||||||
|
Can optionally specify a max and the string will have a maximum length |
||||||
|
|
||||||
|
```js |
||||||
|
chance.string({ max: 20 }); |
||||||
|
=> 'k7fubkfMS@gs#E' |
||||||
|
``` |
||||||
|
|
||||||
|
Can optionally specify a pool and the string will be generated with characters |
||||||
|
only from that pool. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.string({ pool: 'abcde' }); |
||||||
|
=> 'cccdeeabedebb' |
||||||
|
``` |
||||||
|
|
||||||
|
Of course these options can also be combined, using length or min and max. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.string({ length: 5, pool: 'abcde' }); |
||||||
|
=> 'cbbdc' |
||||||
|
``` |
||||||
|
|
||||||
|
```js |
||||||
|
chance.string({ min: 5, max: 20, pool: 'abcde' }); |
||||||
|
=> 'ebddceaaceeda' |
||||||
|
``` |
||||||
|
|
||||||
|
All the options for [chance.character()](./character.md) are supported: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.string({ length: 8, casing: 'upper', alpha: true, numeric: true }); |
||||||
|
=> '3THK7GB1' |
||||||
|
``` |
||||||
@ -0,0 +1,27 @@ |
|||||||
|
# template |
||||||
|
|
||||||
|
Return a random string matching the given template. |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.template('{AA####}') |
||||||
|
=> 'ZQ7803' |
||||||
|
chance.template('{Aa}-{##}') |
||||||
|
=> 'Vr-78' |
||||||
|
chance.template('{####}:{####}:{####}') |
||||||
|
=> '1628:5987:7803' |
||||||
|
``` |
||||||
|
|
||||||
|
The template consists of any number of "character replacement" and "character |
||||||
|
literal" sequences. A "character replacement" sequence starts with a left |
||||||
|
brace, has any number of special replacement characters, and ends with a right |
||||||
|
brace. A character literal can be any character except a brace or a backslash. |
||||||
|
A literal brace or backslash character can be included in the output by |
||||||
|
escaping with a backslash. |
||||||
|
|
||||||
|
The following replacement characters can be used in a replacement sequence: |
||||||
|
|
||||||
|
* "#": a random digit |
||||||
|
* "a": a random lower case letter |
||||||
|
* "A": a random upper case letter |
||||||
|
|
||||||
@ -0,0 +1,108 @@ |
|||||||
|
.markdown-body a { |
||||||
|
color: #c0392b; |
||||||
|
} |
||||||
|
|
||||||
|
.menu { |
||||||
|
background-color: #c0392b; |
||||||
|
} |
||||||
|
|
||||||
|
.-menu-visible .menu-toggle { |
||||||
|
background-color: #c0392b; |
||||||
|
} |
||||||
|
|
||||||
|
.menu-toggle { |
||||||
|
color: #fff; |
||||||
|
background-color: #c0392b; |
||||||
|
} |
||||||
|
|
||||||
|
.toc-menu .link.-active, .toc-menu .hlink.-active { |
||||||
|
box-shadow: inset -2px 0 #fff; |
||||||
|
} |
||||||
|
|
||||||
|
.toc-menu .-level-1.-parent > .title { |
||||||
|
color: #fff; |
||||||
|
} |
||||||
|
|
||||||
|
.toc-menu .-level-1:first-child > .title { |
||||||
|
color: #fff; |
||||||
|
} |
||||||
|
|
||||||
|
ul.heading-list .hlink, ul.heading-list .hlink:visited { |
||||||
|
color: #fff; |
||||||
|
} |
||||||
|
|
||||||
|
.toc-menu .link, .toc-menu .link:visited { |
||||||
|
color: #fff; |
||||||
|
} |
||||||
|
|
||||||
|
ul.heading-list .hlink::before { |
||||||
|
background: #fff; |
||||||
|
} |
||||||
|
|
||||||
|
.footer-nav a { |
||||||
|
color: #c0392b; |
||||||
|
} |
||||||
|
|
||||||
|
.footer-nav .label, .footer-nav .left .title { |
||||||
|
color: #c0392b; |
||||||
|
} |
||||||
|
|
||||||
|
.header-nav.-expanded .icon { |
||||||
|
color: #fff; |
||||||
|
} |
||||||
|
|
||||||
|
.pullquote { |
||||||
|
display: block; |
||||||
|
float: right; |
||||||
|
padding: 0 0 0 10px; |
||||||
|
margin: 0 0 10px 10px; |
||||||
|
width: 15.0em; |
||||||
|
font-size: 1.0em; |
||||||
|
line-height: 1.4em; |
||||||
|
color: #666; |
||||||
|
border-left: 3px solid #ccc; |
||||||
|
} |
||||||
|
|
||||||
|
.markdown-body pre { |
||||||
|
background-color: #fdf6e3; |
||||||
|
border-radius: 12px; |
||||||
|
} |
||||||
|
|
||||||
|
.markdown-body pre code { |
||||||
|
background-color: #fdf6e3; |
||||||
|
} |
||||||
|
|
||||||
|
a.download { |
||||||
|
-webkit-user-select: none; |
||||||
|
background: #fff; |
||||||
|
background-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,32,0.01)), to(rgba(0,0,32,0.08))); |
||||||
|
background-image: -moz-linear-gradient(rgba(0,0,32,0.01), rgba(0,0,32,0.08)); |
||||||
|
-webkit-border-radius: 6px; |
||||||
|
-moz-border-radius: 6px; |
||||||
|
border-radius: 6px; |
||||||
|
color: #202028; |
||||||
|
padding: 4px 10px; |
||||||
|
border: 1px solid #a8a8af; |
||||||
|
text-decoration: none !important; |
||||||
|
text-shadow: 0 1px 1px rgba(255,255,255,0.9); |
||||||
|
font-size: 14px; |
||||||
|
line-height: 19px; |
||||||
|
font-family: helvetica, arial, sans-serif; |
||||||
|
display: inline-block; |
||||||
|
cursor: pointer; |
||||||
|
font-weight: bold; |
||||||
|
} |
||||||
|
|
||||||
|
a.download:hover { |
||||||
|
color: #202028; |
||||||
|
background-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,32,0.01)), to(rgba(0,0,32,0.16))); |
||||||
|
background-image: -moz-linear-gradient(rgba(0,0,32,0.01), rgba(0,0,32,0.16)); |
||||||
|
border-color: #707078; |
||||||
|
} |
||||||
|
|
||||||
|
a.download:active { |
||||||
|
background-color: rgba(0,0,32,0.05); |
||||||
|
background-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,32,0.16)), to(rgba(0,0,32,0.01))); |
||||||
|
background-image: -moz-linear-gradient(rgba(0,0,32,0.16), rgba(0,0,32,0.01)); |
||||||
|
box-shadow: inset 0 2px 2px rgba(0,0,0,0.1); |
||||||
|
} |
||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,37 @@ |
|||||||
|
# cc |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.cc() |
||||||
|
chance.cc({type: 'Mastercard'}) |
||||||
|
``` |
||||||
|
|
||||||
|
<p class="pullquote" data-pullquote='Somewhat obvious warning: Do not use this to hit live payment gateways...' markdown="1"></p> |
||||||
|
|
||||||
|
Generate a random credit card number. This card number will pass the |
||||||
|
[Luhn algorithm][Luhn] so it looks like a legit card. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.cc(); |
||||||
|
=> '6304038511073827' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify a particular type of card to return: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.cc({type: 'Mastercard'}); |
||||||
|
=> '5171206237468496' |
||||||
|
``` |
||||||
|
|
||||||
|
The type can be specified by the long name, or by the short name: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.cc({type: 'mc'}); |
||||||
|
=> '5103820202214116' |
||||||
|
``` |
||||||
|
|
||||||
|
|
||||||
|
The [types][types] are enumerated below. |
||||||
|
|
||||||
|
[Luhn]: http://en.wikipedia.org/wiki/Luhn_algorithm |
||||||
|
[types]: #cc_type |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
# cc_type |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.cc_type() |
||||||
|
chance.cc_type({raw: true}) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random credit card type. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.cc_type(); |
||||||
|
=> 'Visa' |
||||||
|
``` |
||||||
|
|
||||||
|
Default returns just the name. To return the entire object (consisting of name, |
||||||
|
short name, numeric prefix, and length), specify so with the raw flag. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.cc_type({raw: true}); |
||||||
|
=> {name: 'Discover Card', short_name: 'discover', prefix: '6011', length: 16} |
||||||
|
``` |
||||||
|
|
||||||
|
The available types are (name - *short_name*): |
||||||
|
|
||||||
|
* American Express - *amex* |
||||||
|
* Bankcard - *bankcard* |
||||||
|
* China UnionPay - *chinaunion* |
||||||
|
* Diners Club Carte Blanche - *dccarte* |
||||||
|
* Diners Club enRoute - *dcenroute* |
||||||
|
* Diners Club International - *dcintl* |
||||||
|
* Diners Club United States & Canada - *dcusc* |
||||||
|
* Discover Card - *discover* |
||||||
|
* InstaPayment - *instapay* |
||||||
|
* JCB - *jcb* |
||||||
|
* Laser - *laser* |
||||||
|
* Maestro - *maestro* |
||||||
|
* Mastercard - *mc* |
||||||
|
* Solo - *solo* |
||||||
|
* Switch - *switch* |
||||||
|
* Visa - *visa* |
||||||
|
* Visa Electron - *electron* |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# currency |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.currency() |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random currency. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.currency(); |
||||||
|
=> { code: "TVD", name: "Tuvalu Dollar" } |
||||||
|
``` |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# currency_pair |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.currency_pair() |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a currency pair. Handy for simulating currency conversions. Guaranteed to return a unique pair (and not the same currency twice). |
||||||
|
|
||||||
|
```js |
||||||
|
chance.currency_pair(); |
||||||
|
=> [{ code: "ALL", name: "Albania Lek" }, { code: "ZWD", name: "Zimbabwe Dollar" }] |
||||||
|
``` |
||||||
@ -0,0 +1,30 @@ |
|||||||
|
# dollar |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.dollar() |
||||||
|
chance.dollar({max: 250}) |
||||||
|
``` |
||||||
|
|
||||||
|
<p class="pullquote" data-pullquote="Dolla, dolla, bill ya'll" markdown="1"></p> |
||||||
|
|
||||||
|
Return a random dollar amount. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.dollar(); |
||||||
|
=> "$2560.27" |
||||||
|
|
||||||
|
chance.dollar(); |
||||||
|
=> "$750.99" |
||||||
|
``` |
||||||
|
|
||||||
|
By default returns dollar amount no larger than 10000. Optionally specify |
||||||
|
the max to make it larger (or smaller). |
||||||
|
|
||||||
|
```js |
||||||
|
chance.dollar({max: 20}); |
||||||
|
=> "$15.23" |
||||||
|
|
||||||
|
chance.dollar({max: 10000000}) |
||||||
|
=> "$5051205.49" |
||||||
|
``` |
||||||
@ -0,0 +1,28 @@ |
|||||||
|
# euro |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.euro() |
||||||
|
chance.euro({max: 250}) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random euro amount. Formatting depends on the current locale (samples are displayed with european formatting) |
||||||
|
|
||||||
|
```js |
||||||
|
chance.euro(); |
||||||
|
=> "2.560,27€" |
||||||
|
|
||||||
|
chance.euro(); |
||||||
|
=> "750.99€" |
||||||
|
``` |
||||||
|
|
||||||
|
By default returns euro amount no larger than 10000. Optionally specify |
||||||
|
the max to make it larger (or smaller). |
||||||
|
|
||||||
|
```js |
||||||
|
chance.euro({max: 20}); |
||||||
|
=> "15,23€" |
||||||
|
|
||||||
|
chance.euro({max: 10000000}) |
||||||
|
=> "5.051.205,49€" |
||||||
|
``` |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
# exp |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.exp() |
||||||
|
chance.exp({raw: true}) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random credit card expiration. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.exp(); |
||||||
|
=> '10/2020' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify that a raw object be returned rather than a string |
||||||
|
|
||||||
|
```js |
||||||
|
chance.exp({raw: true}); |
||||||
|
=> {month: '11', year: '2017'} |
||||||
|
``` |
||||||
@ -0,0 +1,28 @@ |
|||||||
|
# exp_month |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.exp_month() |
||||||
|
chance.exp_month({future: true}) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random credit card expiration month. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.exp_month(); |
||||||
|
=> '01' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify that it must be a later month than the current month. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.exp_month({future: true}); |
||||||
|
=> '10' |
||||||
|
``` |
||||||
|
|
||||||
|
So if called in June, this would return a random month from July - Dec. If |
||||||
|
called in October, would return November or December. |
||||||
|
|
||||||
|
This because many credit card sandboxes require an expiration date later |
||||||
|
than the current date so it's necessary when generating an expiration with the |
||||||
|
current year to generate a month later than the current month. |
||||||
@ -0,0 +1,15 @@ |
|||||||
|
# exp_year |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.exp_year() |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random credit card expiration year. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.exp_year(); |
||||||
|
=> '2018' |
||||||
|
``` |
||||||
|
|
||||||
|
Returns a random year between today and 10 years in the future. |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# capitalize |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.capitalize(string) |
||||||
|
``` |
||||||
|
|
||||||
|
Capitalize the first letter of a word |
||||||
|
|
||||||
|
```js |
||||||
|
chance.capitalize('bread') |
||||||
|
=> 'Bread' |
||||||
|
``` |
||||||
@ -0,0 +1,90 @@ |
|||||||
|
# mixin |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.mixin(<Object>) |
||||||
|
``` |
||||||
|
|
||||||
|
Mixins are a very powerful way to extend **Chance** to fit the needs of your |
||||||
|
specific application. |
||||||
|
|
||||||
|
First, if you are thinking of using a mixin for **Chance**, please consider first |
||||||
|
whether your use is something from which others may benefit. If so, please |
||||||
|
submit a [pull request][PR] rather than using a mixin! |
||||||
|
|
||||||
|
Spread the love and give everyone the benefit of your awesome generator :) |
||||||
|
|
||||||
|
Now, that said, there are bound to be times when you want to generate something |
||||||
|
random that is specific to your application but not widely applicable. Enter |
||||||
|
mixins! |
||||||
|
|
||||||
|
**Chance** mixins allow you to add one-off methods to **Chance** which you can |
||||||
|
use later. |
||||||
|
|
||||||
|
For example, let's say I have a user object which consists of first, last, |
||||||
|
and email. |
||||||
|
|
||||||
|
```js |
||||||
|
|
||||||
|
var user = { |
||||||
|
first: 'John', |
||||||
|
last: 'Smith', |
||||||
|
email: 'john@aol.com' |
||||||
|
}; |
||||||
|
|
||||||
|
``` |
||||||
|
|
||||||
|
Let's say I want to be able to randomly generate these user objects. |
||||||
|
|
||||||
|
This is not the type of thing which would be widely applicable as it's specific |
||||||
|
to my application so it's perfect for a mixin! |
||||||
|
|
||||||
|
To create a mixin, build an object whose keys are the names of the methods, and |
||||||
|
whose values are the functions to be called. |
||||||
|
|
||||||
|
Note: Within each function, you will have access to `chance` itself! |
||||||
|
|
||||||
|
For example, to create a `user` mixin: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.mixin({ |
||||||
|
'user': function() { |
||||||
|
return { |
||||||
|
first: chance.first(), |
||||||
|
last: chance.last(), |
||||||
|
email: chance.email() |
||||||
|
}; |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
// Then you can call your mixin |
||||||
|
chance.user(); |
||||||
|
|
||||||
|
=> {first: 'Eli', last: 'Benson', email: 'gembibuj@dugesan.com'} |
||||||
|
``` |
||||||
|
|
||||||
|
Mixins can even include other mixins! |
||||||
|
|
||||||
|
For example, to "extend" the user object: |
||||||
|
```js |
||||||
|
chance.mixin({ |
||||||
|
'user': function () { |
||||||
|
return { |
||||||
|
first: chance.first(), |
||||||
|
last: chance.last(), |
||||||
|
email: chance.email() |
||||||
|
}; |
||||||
|
}, |
||||||
|
'social_user': function () { |
||||||
|
var user = chance.user(); |
||||||
|
user.network = chance.pick(['facebook', 'twitter']); |
||||||
|
return user; |
||||||
|
} |
||||||
|
}); |
||||||
|
``` |
||||||
|
|
||||||
|
So we have a second mixin here, `social_user` which is using the `user` mixin |
||||||
|
and adding to it! Note, these mixins can be defined in any order on the object |
||||||
|
if both declared at once. |
||||||
|
|
||||||
|
[PR]: https://github.com/victorquinn/chancejs/pulls |
||||||
@ -0,0 +1,40 @@ |
|||||||
|
# pad |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.pad(number, width) |
||||||
|
chance.pad(number, width, padder) |
||||||
|
``` |
||||||
|
|
||||||
|
Pad a number with some string until it reaches a desired width. |
||||||
|
|
||||||
|
By default, `chance.pad()` will pad with zeroes. For example, to zero-pad |
||||||
|
numbers such that the outcome width is 5, do the following. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.pad(45, 5) |
||||||
|
=> '00045' |
||||||
|
|
||||||
|
chance.pad(284, 5) |
||||||
|
=> '00284' |
||||||
|
|
||||||
|
chance.pad(82843, 5) |
||||||
|
=> '82843' |
||||||
|
``` |
||||||
|
|
||||||
|
Notice how every item returned is a string with leading zeroes until the width |
||||||
|
is 5 for each one. |
||||||
|
|
||||||
|
Can optionally specify a character if the desire is to pad with something other |
||||||
|
than zero. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.pad(81, 5, 'Z') |
||||||
|
=> 'ZZZ81' |
||||||
|
|
||||||
|
chance.pad(692, 5, 'Z') |
||||||
|
=> 'ZZ692' |
||||||
|
|
||||||
|
chance.pad(52859, 5) |
||||||
|
=> '52859' |
||||||
|
``` |
||||||
@ -0,0 +1,9 @@ |
|||||||
|
# pick |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.pick(array) |
||||||
|
chance.pick(array, count) |
||||||
|
``` |
||||||
|
|
||||||
|
**`pick()` is now deprecated in favor of `pickone()` and `pickset()`** |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# pickone |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.pickone(array) |
||||||
|
``` |
||||||
|
|
||||||
|
Given an array, pick a random element and return it |
||||||
|
|
||||||
|
```js |
||||||
|
chance.pickone(['alpha', 'bravo', 'charlie', 'delta', 'echo']); |
||||||
|
=> 'delta' |
||||||
|
``` |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
# pickset |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.pickset(array, quantity) |
||||||
|
``` |
||||||
|
|
||||||
|
Given an array, pick some random elements and return them in a new array |
||||||
|
|
||||||
|
```js |
||||||
|
chance.pickset(['alpha', 'bravo', 'charlie', 'delta', 'echo'], 3); |
||||||
|
=> ['echo', 'alpha', 'bravo'] |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally omit the quantity to retrieve a set with length 1 |
||||||
|
|
||||||
|
```js |
||||||
|
chance.pickset(['alpha', 'bravo', 'charlie', 'delta', 'echo']); |
||||||
|
=> ['delta'] |
||||||
|
``` |
||||||
@ -0,0 +1,23 @@ |
|||||||
|
# set |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.set(key, value) |
||||||
|
``` |
||||||
|
|
||||||
|
Used for overriding the default data used by Chance. |
||||||
|
|
||||||
|
For example, if instead of the default set of last names (which were pulled from the U.S. census data and therefore obviously American biased), you could replace the data for the first names with something more suited to your task. For example, if you want instead to pick from last names of houses in *A Song of Ice and Fire*, you could do something like: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.set('lastNames', ['Arryn', 'Baratheon', 'Bolton', 'Frey', 'Greyjoy', 'Lannister', 'Martell', 'Stark', 'Targaryen', 'Tully', 'Tyrell']); |
||||||
|
|
||||||
|
// then |
||||||
|
chance.last() |
||||||
|
=> 'Lannister' |
||||||
|
``` |
||||||
|
|
||||||
|
This is very handy for internationalization. |
||||||
|
|
||||||
|
Available keys for datasets to override: `firstNames`, `lastNames`, `provinces`, `us_states_and_dc`, `territories`, `armed_forces`, `street_suffixes`, `months`, `cc_types`, `currency_types` |
||||||
|
|
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# shuffle |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.shuffle(array) |
||||||
|
``` |
||||||
|
|
||||||
|
Given an array, scramble the order and return it. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.shuffle(['alpha', 'bravo', 'charlie', 'delta', 'echo']); |
||||||
|
=> ['echo', 'delta', 'alpha', 'charlie', 'bravo'] |
||||||
|
``` |
||||||
|
After Width: | Height: | Size: 28 KiB |
@ -0,0 +1,56 @@ |
|||||||
|
#  |
||||||
|
|
||||||
|
**Chance** is a minimalist generator of random <a href="#true-random">[1]</a> |
||||||
|
strings, numbers, etc. to help reduce some monotony particularly while writing |
||||||
|
automated tests or anywhere else you need anything random. |
||||||
|
|
||||||
|
**Chance** is open source software and is released under the developer and |
||||||
|
business-friendly [MIT License](http://en.wikipedia.org/wiki/MIT_License) |
||||||
|
|
||||||
|
**Chance** is loaded on this site so you can just open the console on your |
||||||
|
browser and play! |
||||||
|
|
||||||
|
 |
||||||
|
|
||||||
|
<a class="twitter-share-button" |
||||||
|
href="https://twitter.com/share" |
||||||
|
data-size="large" |
||||||
|
data-text="Chance - Utility library to generate random things for JavaScript" |
||||||
|
data-hashtags="chancejs,javascript" |
||||||
|
data-url="http://chancejs.com/" |
||||||
|
data-count="horizontal" |
||||||
|
data-via="victorquinn"> |
||||||
|
Tweet |
||||||
|
</a> |
||||||
|
|
||||||
|
<iframe src="https://ghbtns.com/github-btn.html?user=chancejs&repo=chancejs&type=star&count=true" allowtransparency="true" frameborder="0" scrolling="0" width="170px" height="20px"></iframe> |
||||||
|
|
||||||
|
Written by [Victor Quinn](https://www.victorquinn.com) |
||||||
|
|
||||||
|
[](https://nodei.co/npm/chance/) |
||||||
|
|
||||||
|
<ul class=downloads> |
||||||
|
<li> |
||||||
|
<a href="chance.js" class="download" download="chance.js">chance.js (for development)</a> |
||||||
|
- <i>95KB uncompressed</i> |
||||||
|
</li> |
||||||
|
<li> |
||||||
|
<a href="chance.min.js" class="download">chance.min.js (for production)</a> |
||||||
|
– <i>55KB, 17KB when gzipped</i> |
||||||
|
</li> |
||||||
|
</ul> |
||||||
|
|
||||||
|
Or move on to the [Usage](usage/bower.md) section for many other ways to get and use Chance. |
||||||
|
|
||||||
|
## Acknowledgements & Thanks |
||||||
|
|
||||||
|
Thank you to [Sean McCullough](https://github.com/banksean) for your [Mersenne Twister gist](https://gist.github.com/banksean/300494) on which almost the entirety of this library is dependent. And to Takuji Nishimura and Makoto Matsumoto who wrote the original C version on which Sean's JavaScript gist is based. |
||||||
|
|
||||||
|
And of course a monster thank you to all of the [Contributors](https://github.com/chancejs/chancejs/graphs/contributors) to this project! |
||||||
|
|
||||||
|
<small>© 2013 [Victor Quinn](https://www.victorquinn.com) |
||||||
|
Chance and this documentation are released under the terms of the [MIT license](http://opensource.org/licenses/MIT).</small> |
||||||
|
|
||||||
|
<a id="true-random">[1]</a> Of course true mathematicians out there would say that this ought to be considered as only generating pseudo-random things, not true random. For that we'd need some external entropy, not depending on a Mersenne Twister, blah, blah, blah. However, for most practical uses of this library for which it was created and intended, such as generating random names and addresses for automated tests, our random is good enough. But it should probably NOT be used for any cryptographic applications requiring true-random. See [Wikipedia's article](http://vq.io/12hBd84) for more info. |
||||||
|
|
||||||
|
<a href="https://www.gosquared.com/analytics/?utm_campaign=badge"><img src="https://stats.gs/badge" alt="Analytics by GoSquared" /></a> |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
# address |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.address() |
||||||
|
chance.address({short_suffix: true}) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random street address |
||||||
|
|
||||||
|
```js |
||||||
|
chance.address(); |
||||||
|
=> '5447 Bazpe Lane' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally mandate that it return a short suffix |
||||||
|
|
||||||
|
```js |
||||||
|
chance.address({short_suffix: true}); |
||||||
|
=> '536 Baner Rd' |
||||||
|
``` |
||||||
@ -0,0 +1,29 @@ |
|||||||
|
# altitude |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.altitude() |
||||||
|
chance.altitude({ fixed: 7 }) |
||||||
|
chance.altitude({ max: 1000 }) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random altitude, in meters. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.altitude() |
||||||
|
=> 1863.21417 |
||||||
|
``` |
||||||
|
|
||||||
|
By default, includes 5 digits of accuracy after the decimal. Can override with the `fixed` option. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.altitude({ fixed: 7 }) |
||||||
|
=> 6897.8978386 |
||||||
|
``` |
||||||
|
|
||||||
|
By default, max of 8848m (height of Mount Everest), but this can be overridden with the `max` option. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.altitude({ max: 1000 }) |
||||||
|
=> 890.20665 |
||||||
|
``` |
||||||
@ -0,0 +1,16 @@ |
|||||||
|
# areacode |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.areacode() |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random area code |
||||||
|
|
||||||
|
```js |
||||||
|
chance.areacode() |
||||||
|
=> '(526)' |
||||||
|
``` |
||||||
|
|
||||||
|
*Note, this is a US area code, we have little support for internationalization |
||||||
|
at this time. Hope to fix that in the future!* |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# city |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.city() |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random city name |
||||||
|
|
||||||
|
```js |
||||||
|
chance.city(); |
||||||
|
=> 'Cowotba' |
||||||
|
``` |
||||||
@ -0,0 +1,34 @@ |
|||||||
|
# coordinates |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.coordinates() |
||||||
|
chance.coordinates({fixed: 2}) |
||||||
|
chance.coordinates({format: 'dms'}) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate random coordinates, which are latitude and longitude, comma separated. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.coordinates(); |
||||||
|
=> "-29.52974, 24.52815" |
||||||
|
``` |
||||||
|
|
||||||
|
By default includes 5 fixed digits after decimal, can specify otherwise. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.coordinates({fixed: 2}); |
||||||
|
=> "-49.16, 68.81" |
||||||
|
``` |
||||||
|
|
||||||
|
By default cooridnates' format is dd, can specify otherwise. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.coordinates({format: 'ddm'}); |
||||||
|
=> "41°44.9592, 25°56.2622" |
||||||
|
``` |
||||||
|
|
||||||
|
```js |
||||||
|
chance.coordinates({format: 'dms'}); |
||||||
|
=> "56°2’9.8187”, 79°55’40.6812”" |
||||||
|
``` |
||||||
@ -0,0 +1,23 @@ |
|||||||
|
# country |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.country() |
||||||
|
chance.country({ full: true }) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random country. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.country(); |
||||||
|
=> 'LT' |
||||||
|
``` |
||||||
|
|
||||||
|
By default, returns only the 2 letter ISO 3166-1 code for the country. |
||||||
|
|
||||||
|
Optionally specify that it ought to return a full country name. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.country({ full: true }); |
||||||
|
=> 'Venezuela' |
||||||
|
``` |
||||||
@ -0,0 +1,29 @@ |
|||||||
|
# depth |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.depth() |
||||||
|
chance.depth({ fixed: 2 }) |
||||||
|
chance.depth({ min: -1000 }) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random depth, in meters. Depths are always negative |
||||||
|
|
||||||
|
```js |
||||||
|
chance.depth() |
||||||
|
=> -2126.95039 |
||||||
|
``` |
||||||
|
|
||||||
|
By default, includes 5 digits of accuracy after the decimal. Can override with the `fixed` option. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.depth({ fixed: 2 }) |
||||||
|
=> -1542.11 |
||||||
|
``` |
||||||
|
|
||||||
|
By default, maximum (or minimum depending on your frame of reference) depth of -2550 (depth of the Mariana Trench), but this can be overridden with the `min` option. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.depth({ min: -1000 }) |
||||||
|
=> -718.41976 |
||||||
|
``` |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
# geohash |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.geohash() |
||||||
|
chance.geohash({ length: 5 }) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random geohash. [More details on what a geohash is](http://en.wikipedia.org/wiki/Geohash). |
||||||
|
|
||||||
|
```js |
||||||
|
chance.geohash() |
||||||
|
=> 'h9xhn7y' |
||||||
|
``` |
||||||
|
|
||||||
|
By default, includes 7 characters of accuracy. Can override with the `length` option. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.geohash({ length: 5 }) |
||||||
|
=> 'dr0kr' |
||||||
|
``` |
||||||
@ -0,0 +1,43 @@ |
|||||||
|
# latitude |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.latitude() |
||||||
|
chance.latitude({fixed: 7}) |
||||||
|
chance.latitude({format: 'dms'}) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random latitude. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.latitude(); |
||||||
|
=> 57.99514 |
||||||
|
``` |
||||||
|
|
||||||
|
_range: -90 to 90_ |
||||||
|
|
||||||
|
By default includes 5 fixed digits after decimal, can specify otherwise. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.latitude({fixed: 7}); |
||||||
|
=> -29.6443133 |
||||||
|
``` |
||||||
|
|
||||||
|
By default includes entire range of allowed latitudes, can specify a min and/or max to bound it |
||||||
|
|
||||||
|
```js |
||||||
|
chance.latitude({min: 38.7, max: 38.9}); |
||||||
|
=> 38.82358 |
||||||
|
``` |
||||||
|
|
||||||
|
By default latitudes' format is dd, can specify otherwise. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.latitude({format: 'ddm'}); |
||||||
|
=> "41°44.9592" |
||||||
|
``` |
||||||
|
|
||||||
|
```js |
||||||
|
chance.latitude({format: 'dms'}); |
||||||
|
=> "56°2’9.8187”" |
||||||
|
``` |
||||||
@ -0,0 +1,25 @@ |
|||||||
|
# locale |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.locale() |
||||||
|
chance.locale({region: true}) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random ISO-639-1 language code |
||||||
|
|
||||||
|
```js |
||||||
|
chance.locale(); |
||||||
|
=> 'ca' |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random IETF region code |
||||||
|
|
||||||
|
```js |
||||||
|
chance.locale({region: true}); |
||||||
|
=> 'es-EA' |
||||||
|
``` |
||||||
|
|
||||||
|
|
||||||
|
_Note, the language codes comes from the [ISO-639-1 spec](http://www.loc.gov/standards/iso639-2/php/code_list.php) |
||||||
|
and the region codes come from [IETF standard](http://data.okfn.org/data/core/language-codes#resource-language-codes-full)_ |
||||||
@ -0,0 +1,43 @@ |
|||||||
|
# longitude |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.longitude() |
||||||
|
chance.longitude({fixed: 7}) |
||||||
|
chance.longitude({format: 'dms'}) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random longitude. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.longitude(); |
||||||
|
=> 149.41549 |
||||||
|
``` |
||||||
|
|
||||||
|
_range: -180 to 180_ |
||||||
|
|
||||||
|
By default includes 5 fixed digits after decimal, can specify otherwise. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.longitude({fixed: 7}); |
||||||
|
=> 51.4549925 |
||||||
|
``` |
||||||
|
|
||||||
|
By default includes entire range of allowed longitudes, can specify a min and/or max to bound it |
||||||
|
|
||||||
|
```js |
||||||
|
chance.longitude({min: -78, max: -77}); |
||||||
|
=> -77.22644 |
||||||
|
``` |
||||||
|
|
||||||
|
By default longitude' format is dd, can specify otherwise. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.longitude({format: 'ddm'}); |
||||||
|
=> "41°44.9592" |
||||||
|
``` |
||||||
|
|
||||||
|
```js |
||||||
|
chance.longitude({format: 'dms'}); |
||||||
|
=> "56°2’9.8187”" |
||||||
|
``` |
||||||
@ -0,0 +1,48 @@ |
|||||||
|
# phone |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.phone() |
||||||
|
chance.phone({ formatted: false }) |
||||||
|
chance.phone({ country: "fr" }) |
||||||
|
chance.phone({ country: "fr", mobile: true }) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random phone |
||||||
|
|
||||||
|
```js |
||||||
|
chance.phone(); |
||||||
|
=> '(494) 927-2152' |
||||||
|
``` |
||||||
|
|
||||||
|
By default conforms to [NANP](http://en.wikipedia.org/wiki/North_American_Numbering_Plan) for a proper US phone number. |
||||||
|
|
||||||
|
Optionally disable formatting. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.phone({ formatted: false }); |
||||||
|
=> '2617613391' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify a country. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.phone({ country: 'fr' }); |
||||||
|
=> '01 60 44 92 67' |
||||||
|
``` |
||||||
|
|
||||||
|
Note, at current we only have support for `'us'`, `'uk'`, or `'fr'` for countries. |
||||||
|
|
||||||
|
For `uk` and `fr`, optionally specify a mobile phone. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.phone({ country: 'uk', mobile: true }); |
||||||
|
=> '07624 321221' |
||||||
|
``` |
||||||
|
|
||||||
|
For `us`, optionally specify an exampleNumber for a '555' area code. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.phone({ country: 'us', exampleNumber: true }); |
||||||
|
=> '(555) 927-2152' |
||||||
|
``` |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# postal |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.postal() |
||||||
|
``` |
||||||
|
|
||||||
|
Return a Canadian Postal code. Returned postal code is valid with respect to the Postal District (first character) and format only. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.postal(); |
||||||
|
=> 'R1S 3F4' |
||||||
|
``` |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# postcode |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.postcode() |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random (U.K.) postcode. Returned postcode is valid with respect to the Postcode Area (first characters) and format only. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.postcode(); |
||||||
|
=> 'W6 9PF' |
||||||
|
``` |
||||||
@ -0,0 +1,33 @@ |
|||||||
|
# province |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.province() |
||||||
|
chance.province({full: true}) |
||||||
|
chance.province({full: true}) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random province. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.province(); |
||||||
|
=> 'ON' |
||||||
|
``` |
||||||
|
|
||||||
|
By default, returns only the 2 letter abbreviation for province. |
||||||
|
|
||||||
|
Optionally specify that it ought to return a full province name. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.province({full: true}); |
||||||
|
=> 'Nova Scotia' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify the country from which it should return the province name. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.province({country: 'it', full: true}); |
||||||
|
=> 'Vicenza' |
||||||
|
``` |
||||||
|
|
||||||
|
Note, currently support for country is limited to: `'ca', 'it'`. |
||||||
@ -0,0 +1,62 @@ |
|||||||
|
# state |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.state() |
||||||
|
chance.state({ full: true }) |
||||||
|
chance.state({ territories: true }) |
||||||
|
chance.state({ armed_forces: true }) |
||||||
|
chance.state({ us_states_and_dc: false }) |
||||||
|
chance.state({ country: 'us' }) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random state. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.state(); |
||||||
|
=> 'AK' |
||||||
|
``` |
||||||
|
|
||||||
|
By default, returns only the 2 letter abbreviation for state. |
||||||
|
|
||||||
|
Optionally specify that it ought to return a full state name. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.state({ full: true }); |
||||||
|
=> 'Florida' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally add U.S. Territories ('American Samoa', 'Federated States of Micronesia', 'Guam', 'Marshall Islands', 'Northern Mariana Islands', 'Puerto Rico', 'Virgin Islands, U.S.') to the mix of randomly selected items: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.state({ territories: true, full: true }) |
||||||
|
=> 'Guam' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally add Armed Forces to the list as well: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.state({ armed_forces: true, full: true }) |
||||||
|
=> 'Armed Forces Pacific' |
||||||
|
``` |
||||||
|
|
||||||
|
For all U.S. states, territories, and armed forces, specify all of them: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.state({ armed_forces: true, territories: true }) |
||||||
|
=> 'NY' |
||||||
|
``` |
||||||
|
|
||||||
|
For just territories or armed forces, specify that it ought not return U.S. states: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.state({ territories: true, us_states_and_dc: false }) |
||||||
|
=> 'PR' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify a country (US specific options are ignored if country is specified and different from `'us'`): |
||||||
|
|
||||||
|
```js |
||||||
|
chance.state({ country: 'it', full: true }) |
||||||
|
=> 'Toscana' |
||||||
|
``` |
||||||
@ -0,0 +1,37 @@ |
|||||||
|
# street |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.street() |
||||||
|
chance.street({country:it}) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random street |
||||||
|
|
||||||
|
```js |
||||||
|
chance.street(); |
||||||
|
=> 'Tesca Circle' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally mandate that it returns a short suffix |
||||||
|
|
||||||
|
```js |
||||||
|
chance.street({short_suffix: true}); |
||||||
|
=> 'Jiled St' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify the number of syllables used to generate the street name |
||||||
|
|
||||||
|
```js |
||||||
|
chance.street({syllables: 8}); |
||||||
|
=> 'Teniefitinusewjircor Junction' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify a country to localize street prefixes |
||||||
|
|
||||||
|
```js |
||||||
|
chance.street({country: 'it'}); |
||||||
|
=> 'Via Nefba' |
||||||
|
``` |
||||||
|
|
||||||
|
Note, currently support for country is limited to: `'us', 'it'`. |
||||||
@ -0,0 +1,23 @@ |
|||||||
|
# zip |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.zip() |
||||||
|
chance.zip({plusfour: true}) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a random (U.S.) zip code. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.zip(); |
||||||
|
=> '90210' |
||||||
|
``` |
||||||
|
|
||||||
|
Can optionally specify that it ought to return a [Zip+4][zip+4]: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.zip({plusfour: true}); |
||||||
|
=> '01035-1838' |
||||||
|
``` |
||||||
|
|
||||||
|
[zip+4]: http://vq.io/19rzsve |
||||||
|
After Width: | Height: | Size: 64 KiB |
@ -0,0 +1,16 @@ |
|||||||
|
# coin |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.coin() |
||||||
|
``` |
||||||
|
|
||||||
|
Flip a coin! |
||||||
|
|
||||||
|
```js |
||||||
|
chance.coin(); |
||||||
|
=> 'heads' |
||||||
|
|
||||||
|
chance.coin(); |
||||||
|
=> 'tails' |
||||||
|
``` |
||||||
@ -0,0 +1,29 @@ |
|||||||
|
# dice |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.d4() |
||||||
|
chance.d6() |
||||||
|
chance.d8() |
||||||
|
chance.d10() |
||||||
|
chance.d12() |
||||||
|
chance.d20() |
||||||
|
chance.d30() |
||||||
|
chance.d100() |
||||||
|
``` |
||||||
|
|
||||||
|
<p class="pullquote" data-pullquote='"Any dungeon master worth his weight in geldings goes nowhere without his 20 sided die."' markdown="1"></p> |
||||||
|
|
||||||
|
Return a value equal to the roll of a die. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.d20(); |
||||||
|
=> 13 |
||||||
|
|
||||||
|
chance.d6(); |
||||||
|
=> 4 |
||||||
|
``` |
||||||
|
|
||||||
|
These are just wrappers around natural() but are convenient for use by games. |
||||||
|
|
||||||
|
They return values between 1 and the number after the `d`, so `chance.d4()` returns 1, 2, 3, or 4, just like a 4 sided die would. |
||||||
@ -0,0 +1,36 @@ |
|||||||
|
# File with content |
||||||
|
|
||||||
|
Generates a file with random name, extention and data |
||||||
|
```js |
||||||
|
// Usage |
||||||
|
chance.fileWithContent({ |
||||||
|
fileName: "AwesomeFile", |
||||||
|
fileExtension: "gif", |
||||||
|
fileSize: 1024}) |
||||||
|
|
||||||
|
``` |
||||||
|
|
||||||
|
Returns an object with fileData as a Buffer and file name as a String |
||||||
|
```js |
||||||
|
// Returns |
||||||
|
{ |
||||||
|
fileData: <Buffer 00 00 00... 1998 more bytes>, |
||||||
|
fileName: 'zo.gif' |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
Random name and format |
||||||
|
```js |
||||||
|
chance.fileWithContent({fileSize: 1024}) |
||||||
|
|
||||||
|
``` |
||||||
|
Random format |
||||||
|
```js |
||||||
|
chance.fileWithContent({ |
||||||
|
fileName: "AwesomeFile", |
||||||
|
fileSize: 2048}) |
||||||
|
``` |
||||||
|
* Takes the optional fileName and fileExtension, otherwise generates from file() |
||||||
|
* 0 Size files are ok |
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,16 @@ |
|||||||
|
# guid |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.guid() |
||||||
|
chance.guid({version: 5}) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random guid, version 5 by default. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.guid(); |
||||||
|
=> 'f0d8368d-85e2-54fb-73c4-2d60374295e3' |
||||||
|
chance.guid({version: 4}); |
||||||
|
=> 'c71f58e3-34af-43c0-b405-2764d6947d21' |
||||||
|
``` |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
# hash |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.hash() |
||||||
|
chance.hash({length: 25}) |
||||||
|
chance.hash({casing: 'upper'}) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random hex hash |
||||||
|
|
||||||
|
```js |
||||||
|
chance.hash() |
||||||
|
=> 'e5162f27da96ed8e1ae51def1ba643b91d2581d8' |
||||||
|
``` |
||||||
|
|
||||||
|
By default, the hash is lowercase and 40 hex characters long (same as a git |
||||||
|
commit hash). |
||||||
|
|
||||||
|
Optionally specify a length |
||||||
|
|
||||||
|
```js |
||||||
|
chance.hash({length: 15}) |
||||||
|
=> 'c28f57cb599ada4' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify casing to get a hash with only uppercase letters |
||||||
|
rather than the default lowercase |
||||||
|
|
||||||
|
```js |
||||||
|
chance.hash({casing: 'upper'}) |
||||||
|
=> '3F2EB3FB85D88984C1EC4F46A3DBE740B5E0E56E' |
||||||
|
``` |
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,59 @@ |
|||||||
|
# hidden |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.cc_types() |
||||||
|
chance.mersenne_twister() |
||||||
|
chance.mersenne_twister(12345) |
||||||
|
chance.months() |
||||||
|
chance.name_prefixes() |
||||||
|
chance.provinces() |
||||||
|
chance.states() |
||||||
|
chance.street_suffix() |
||||||
|
chance.street_suffixes() |
||||||
|
``` |
||||||
|
|
||||||
|
These aren't really hidden per se, but just utility methods intended to be used |
||||||
|
internally but exposed externally in case they're useful to anyone. |
||||||
|
|
||||||
|
```js |
||||||
|
|
||||||
|
// Return the list of cc types |
||||||
|
chance.cc_types() |
||||||
|
=> [{name: "American Express", short_name: 'amex', prefix: '34', length: 15}, ...] |
||||||
|
|
||||||
|
// Return a new Mersenne Twister |
||||||
|
chance.mersenne_twister() |
||||||
|
=> [object Object] // An instance of a Mersenne Twister |
||||||
|
|
||||||
|
// Optionally provide a seed for that twister |
||||||
|
chance.mersenne_twister(12345) |
||||||
|
=> [object Object] // An instance of a twister with seed 12345 |
||||||
|
|
||||||
|
// Return our list of name prefixes |
||||||
|
chance.months(); |
||||||
|
=> [{name: 'January', short_name 'Jan', numeric: '01'}, ...] |
||||||
|
|
||||||
|
// Return our list of name prefixes |
||||||
|
chance.name_prefixes(); |
||||||
|
=> [{name: 'Doctor', abbreviation: 'Dr.'}, {name: 'Miss', abbreviation: 'Miss'}, ...] |
||||||
|
|
||||||
|
// Return the list of provinces |
||||||
|
chance.provinces(); |
||||||
|
=> [{name: 'Alberta', abbreviation: 'AB'}, {name: 'British Columbia', abbreviation: 'BC'}, ...] |
||||||
|
|
||||||
|
// Return the list of states |
||||||
|
chance.states(); |
||||||
|
=> [{name: 'Alabama', abbreviation: 'AL'}, {name: 'Alaska', abbreviation: 'AK'}, ...] |
||||||
|
|
||||||
|
// Return a random street suffix |
||||||
|
chance.street_suffix(); |
||||||
|
=> {name: 'Street', abbreviation: 'St'} |
||||||
|
|
||||||
|
// Return the list of street suffixes |
||||||
|
chance.street_suffixes(); |
||||||
|
=> [{name: 'Avenue', abbreviation: 'Ave'}, {name: 'Boulevard', abbreviation: 'Blvd'}, ...] |
||||||
|
|
||||||
|
``` |
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,35 @@ |
|||||||
|
# n |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.n(chance.email, 5) |
||||||
|
chance.n(chance.email, 5, { domain: "socialradar.com" }) |
||||||
|
``` |
||||||
|
|
||||||
|
Provide any function that generates random stuff (usually another **Chance** function) and a number and `n()` will generate an array of items with a length matching the length you specified. |
||||||
|
|
||||||
|
For example, to generate 5 email addresses: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.n(chance.email, 5); |
||||||
|
=> [ 'nese@me.gov', |
||||||
|
'tukvogi@novew.co.uk', |
||||||
|
'worzi@jotok.edu', |
||||||
|
'wicumafom@lalu.edu', |
||||||
|
'hifebwo@abecusa.com' ] |
||||||
|
``` |
||||||
|
|
||||||
|
Any options that would be sent to the random function can be added following the number. |
||||||
|
|
||||||
|
For example, `chance.email()` has options which can be specified, so you can generate 5 emails with a known domain as follows: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.n(chance.email, 5, { domain: "socialradar.com" }) |
||||||
|
=> [ 'nuvvu@socialradar.com', |
||||||
|
'icolul@socialradar.com', |
||||||
|
'rig@socialradar.com', |
||||||
|
'ca@socialradar.com', |
||||||
|
'uc@socialradar.com' ] |
||||||
|
``` |
||||||
|
|
||||||
|
Note, these items are not guaranteed to be unique. If that is the intent, see [`chance.unique()`](http://chancejs.com/#unique) |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
# normal |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.normal() |
||||||
|
chance.normal({mean: 100}) |
||||||
|
chance.normal({mean: 100, dev: 15}) |
||||||
|
``` |
||||||
|
|
||||||
|
Return a normally-distributed random variate. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.normal() |
||||||
|
=> 0.4244767651300604 |
||||||
|
``` |
||||||
|
|
||||||
|
By default this starts with a mean of `0` and a standard deviation of `1` which |
||||||
|
is the standard normal distribution. |
||||||
|
|
||||||
|
Optionally specify a mean and/or deviation. |
||||||
|
|
||||||
|
```js |
||||||
|
// Notice, since no deviation was specified, using the default of `1` |
||||||
|
chance.normal({mean: 100}) |
||||||
|
=> 99.68352269988522 |
||||||
|
|
||||||
|
// For example, to get a random IQ (which by definition has a mean of 100 |
||||||
|
// and a standard deviation of 15) |
||||||
|
chance.normal({mean: 100, dev: 15}) |
||||||
|
=> 85.11040121833615 |
||||||
|
``` |
||||||
|
|
||||||
|
Used in combination with the above generators, this can be an extremely powerful |
||||||
|
way to get more realistic results as often "pure random" results fail to |
||||||
|
approximate the real world. |
||||||
@ -0,0 +1,27 @@ |
|||||||
|
# radio |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.radio() |
||||||
|
``` |
||||||
|
|
||||||
|
<p class="pullquote" data-pullquote='Broadcast call signs start with a W if east of the Mississippi River and K if west.' markdown="1"></p> |
||||||
|
|
||||||
|
Generate a random radio call sign. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.radio(); |
||||||
|
=> 'KCXW' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify a side of the Mississippi River to limit stations to that side. |
||||||
|
|
||||||
|
See [K and W](http://en.wikipedia.org/wiki/Call_signs_in_North_America#K_and_W) for more details |
||||||
|
|
||||||
|
```js |
||||||
|
chance.radio({side: 'east'}); |
||||||
|
=> 'WKOQ' |
||||||
|
|
||||||
|
chance.radio({side: 'east'}); |
||||||
|
=> 'WNOW' |
||||||
|
``` |
||||||
@ -0,0 +1,25 @@ |
|||||||
|
# rpg |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.rpg('#d#') |
||||||
|
chance.rpg('#d#', {sum: true}) |
||||||
|
``` |
||||||
|
|
||||||
|
Given an input looking like #d#, where the first # is the number of dice to |
||||||
|
roll and the second # is the max of each die, returns an array of dice values. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.rpg('3d10'); |
||||||
|
=> [1, 6, 9] |
||||||
|
|
||||||
|
chance.rpg('5d6'); |
||||||
|
=> [3, 1, 2, 5, 2] |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify a sum be returned rather than an array of dice. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.rpg('3d10', {sum: true}); |
||||||
|
=> 14 |
||||||
|
``` |
||||||
@ -0,0 +1,25 @@ |
|||||||
|
# tv |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.tv() |
||||||
|
chance.tv({side: 'west'}) |
||||||
|
``` |
||||||
|
|
||||||
|
Generate a TV station call sign. This is an alias for `radio()` since they both |
||||||
|
follow the same rules. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.radio(); |
||||||
|
=> 'WXTY' |
||||||
|
``` |
||||||
|
|
||||||
|
Optionally specify a side of the Mississippi River to limit stations to that side. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.radio({side: 'west'}); |
||||||
|
=> 'KCYL' |
||||||
|
|
||||||
|
chance.radio({side: 'west'}); |
||||||
|
=> 'KQDV' |
||||||
|
``` |
||||||
@ -0,0 +1,47 @@ |
|||||||
|
# unique |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.unique(chance.state, 5) |
||||||
|
chance.unique(chance.state, 5, { comparator: func }) |
||||||
|
``` |
||||||
|
|
||||||
|
Provide any function that generates random stuff (usually another **Chance** function) and a number and `unique()` will generate a random array of unique (not repeating) items with a length matching the one you specified. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.unique(chance.state, 5); |
||||||
|
=> ["SC", "WA", "CO", "TX", "ND"] |
||||||
|
``` |
||||||
|
|
||||||
|
This is helpful when there are a limited number of options and you want a bunch but want to ensure each is different. |
||||||
|
|
||||||
|
Optionally specify the comparator used to determine whether a generated item is in the list of already generated items. By default the comparator just checks to see if the newly generated item is in the array of already generated items. This works for most simple cases (such as `chance.state()`) but will not work if the generated item is an object (because the `Array.prototype.indexOf()` method will not work on an object since 2 objects will not be strictly equal, `===`, unless they are references to the same object). |
||||||
|
|
||||||
|
```js |
||||||
|
chance.unique(chance.currency, 2, { |
||||||
|
comparator: function(arr, val) { |
||||||
|
return arr.reduce(function(acc, item) { |
||||||
|
return acc || (item.code === val.code); |
||||||
|
}, false); |
||||||
|
} |
||||||
|
}); |
||||||
|
=> [{ code: "KYF", name: "Cayman Islands Dollar" }, { code: "CDF", name: "Congo/Kinshasa Franc" }] |
||||||
|
``` |
||||||
|
|
||||||
|
You can also specify any arbitrary options in this third argument and they'll be passed along to the method you specify as the first. |
||||||
|
|
||||||
|
For example, let's say you want to retrieve 10 unique integers between 0 and 100. This is easily achievable by specifying `chance.integer` as hte function, 10 as the number to retrieve, and a min/max in the options. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.unique(chance.integer, 10, {min: 0, max: 100}); |
||||||
|
=> [78, 49, 7, 87, 59, 89, 84, 62, 60, 63] |
||||||
|
``` |
||||||
|
|
||||||
|
Note, there could be cases where it is impossible to generate the unique number. For example, if you choose `chance.state` as shown above as the random function and want say, 55 uniques, **Chance** will throw a RangeError because it is impossible to generate 55 uniques because there are only 51 states in the available pool (50 states plus the District of Columbia). |
||||||
|
|
||||||
|
```js |
||||||
|
chance.unique(chance.state, 55); |
||||||
|
=> RangeError: Chance: num is likely too large for sample set |
||||||
|
``` |
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,55 @@ |
|||||||
|
# weighted |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.weighted(['a', 'b'], [100, 1]) |
||||||
|
chance.weighted(['a', 'b', 'c', 'd'], [1, 2, 3, 4]) |
||||||
|
``` |
||||||
|
|
||||||
|
Provide an array of items, and another array of items specifying the relative weights and **Chance** will select one of those items, obeying the specified weight. |
||||||
|
|
||||||
|
For example, the following code: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.weighted(['a', 'b'], [100, 1]); |
||||||
|
=> 'a' |
||||||
|
``` |
||||||
|
|
||||||
|
Will generate `'a'` 100 times more often than `'b'` but still choose one or the other randomly. |
||||||
|
|
||||||
|
The weights are all relative, so if you have more than just two it will ensure that all items are generated relative to all of the weights. |
||||||
|
|
||||||
|
For example, the following code: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.weighted(['a', 'b', 'c', 'd'], [1, 2, 3, 4]); |
||||||
|
=> 'c' |
||||||
|
``` |
||||||
|
|
||||||
|
Will generate a letter from the array but will pick `'b'` twice as often as it picks `'a'` and will pick `'c'` three times as often as it picks `'a'` and will pick `'d'` four times as often as it will pick `'a'` and will pick `'d'` two times as often as it will pick `'b'`. |
||||||
|
|
||||||
|
The weights can be whole numbers as shown above or fractions. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.weighted(['a', 'b', 'c', 'd'], [0.1, 0.2, 0.3, 0.4]); |
||||||
|
=> 'd' |
||||||
|
``` |
||||||
|
|
||||||
|
There is no requirement that the weights sum to anything in particular, they are all compared relative to each other so all of the following are equivalent: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.weighted(['a', 'b', 'c', 'd'], [1, 2, 3, 4]); |
||||||
|
chance.weighted(['a', 'b', 'c', 'd'], [0.1, 0.2, 0.3, 0.4]); |
||||||
|
chance.weighted(['a', 'b', 'c', 'd'], [100, 200, 300, 400]); |
||||||
|
chance.weighted(['a', 'b', 'c', 'd'], [17, 34, 51, 68]); |
||||||
|
chance.weighted(['a', 'b', 'c', 'd'], [0.17, 0.34, 0.51, 0.68]); |
||||||
|
``` |
||||||
|
|
||||||
|
Recall JavaScript has first class functions so you could do something like the following: |
||||||
|
|
||||||
|
```js |
||||||
|
chance.weighted([chance.fbid, chance.twitter, chance.ip], [10, 5, 1])(); |
||||||
|
=> 10000345166213 |
||||||
|
``` |
||||||
|
|
||||||
|
That will pick one of the **Chance** methods with the relative weights specified and then immediately invoke it, so it will return a random fbid twice as often as it will return a twitter handle (because 10/5 is 2) and an fbid 10 times more often than it will return a random ip address (because 10/1 is 10). It will return a random twitter handle 5 times more often than it will return an ip address (because 5/1 is 5). |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# android_id |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.android_id() |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random GCM registration ID. |
||||||
|
|
||||||
|
```js |
||||||
|
chance.android_id() |
||||||
|
=> 'APA91HHQyPlWqV2Nu61LRs41eE4vrR4bHLKTs0-Dh5nDLopcWZotLw77NEoJyADNJiq6cwY0jMM02y8aacLs6fe2_ynweFjZJVVevKON-32826v-EFoayyThU3-42YEUY9pCScU_n73yRNSOlTk5W6iPtrDkQ3a6_BvOxRbSYi3E6QEY0ZuIQF0' |
||||||
|
``` |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# apple_token |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.apple_token() |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random Apple Push Token |
||||||
|
|
||||||
|
```js |
||||||
|
chance.apple_token() |
||||||
|
=> 'b50edac575bfba07dd019b28b2af7189a3ddda17c806ef14a9abbfd00533f67e' |
||||||
|
``` |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# bb_pin |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.bb_pin() |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random BlackBerry Device PIN |
||||||
|
|
||||||
|
```js |
||||||
|
chance.bb_pin() |
||||||
|
=> '985de771' |
||||||
|
``` |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# wp7_anid |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.wp7_anid() |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random Windows Phone 7 ANID |
||||||
|
|
||||||
|
```js |
||||||
|
chance.wp7_anid() |
||||||
|
=> 'A=3FC2491A0E0C5AB7824B2F60DCE4DB02&E=4e7&W=6' |
||||||
|
``` |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
# wp8_anid2 |
||||||
|
|
||||||
|
```js |
||||||
|
// usage |
||||||
|
chance.wp8_anid2() |
||||||
|
``` |
||||||
|
|
||||||
|
Return a random Windows Phone 8 ANID2 |
||||||
|
|
||||||
|
```js |
||||||
|
chance.wp8_anid2() |
||||||
|
=> 'OGI5ODk0MmNkZGI2OGE3YzAwODE1Y2NiYTc4MzEzZjM=' |
||||||
|
``` |
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue