Skip to content

4. Recipes

Jared Van Valkengoed edited this page Oct 7, 2025 · 6 revisions

Tarot.js Custom Deck with Reversals (Without Separate Reversed Cards)

This example shows how to handle card reversals in a tarot deck without needing to create separate cards like "The Fool Reversed". Instead, we'll keep each card with a single name (e.g., "The Fool") and store both upright and reversed meanings. When performing a reading, we’ll select the appropriate meaning based on whether the card is drawn upright or reversed.

Key Steps

  1. Import Tarot.js Import the Tarot.js library:

    import Tarot from "https://esm.sh/gh/MarketingPipeline/Tarot.js";
  2. Initialize Tarot and Custom Deck Initialize a Tarot instance and define your custom tarot deck. Each card will include both upright and reversed meanings inside a meanings object. The card itself will retain its name, so you don't need separate cards for reversed versions like "The Fool Reversed".

    const tarot = new Tarot();
    
    const your_own_custom_deck = tarot.initializeDeck([
      {
        name: "The Fool",
        meanings: {
          upright: "New beginnings, innocence",
          reversed: "Recklessness, naivety"
        }
      },
      {
        name: "The Coder",
        meanings: {
          upright: "Tired, broke, curious",
          reversed: "Burnout, blocked creativity, frustration"
        }
      },
      {
        name: "The Magician",
        meanings: {
          upright: "Skill, resourcefulness, power",
          reversed: "Manipulation, deceit, illusion"
        }
      },
      {
        name: "The High Priestess",
        meanings: {
          upright: "Wisdom, intuition, mystery",
          reversed: "Secrets, disconnection from intuition"
        }
      }
    ]);
    • Each card in your custom deck includes the meanings property with both upright and reversed definitions.
  3. Create and Add a Custom Spread Create your own tarot spread, specifying the positions and their associated meanings. This example uses a simple "Shakespeare" spread with two positions: "To be" and "Not to be."

    tarot.addSpread("shakespeare", {
      positions: ["To be", "Not to be"],
      description: "Random example spread"
    });
  4. Assigning Reversals Define a function to assign the reversed property to each card in your deck. In this case, we use a 50% chance to randomly assign reversed: true.

    function assignReversals(deck) {
      // Add a `reversed` property to each card
      return deck.map(card => {
        const isReversed = Math.random() > 0.5;  // 50% chance to reverse the card
        return {
          ...card.card,  // Copy the card object
          reversed: isReversed  // Add `reversed` property
        };
      });
    }
    • This function will randomly reverse cards. Each card will retain its name and meanings, but we add the reversed property to indicate if the card is reversed.
  5. Performing a Reading with Reversals Use the tarot.doReading() method to draw a spread (e.g., "shakespeare"). Then, apply the assignReversals() function to add the reversed state to the cards.

    // Perform the "shakespeare" spread and add reversals
    const deckWithReversals = assignReversals(tarot.doReading("shakespeare"));
    
    console.log(deckWithReversals);

    Example Output: After running the reading, each card will include the reversed state along with its upright or reversed meaning:

    [
      {
        name: "The Fool",
        meanings: {
          upright: "New beginnings, innocence",
          reversed: "Recklessness, naivety"
        },
        reversed: true
      },
      {
        name: "The Coder",
        meanings: {
          upright: "Tired, broke, curious",
          reversed: "Burnout, blocked creativity, frustration"
        },
        reversed: false
      },
      {
        name: "The Magician",
        meanings: {
          upright: "Skill, resourcefulness, power",
          reversed: "Manipulation, deceit, illusion"
        },
        reversed: true
      },
      {
        name: "The High Priestess",
        meanings: {
          upright: "Wisdom, intuition, mystery",
          reversed: "Secrets, disconnection from intuition"
        },
        reversed: false
      }
    ]
  6. Selecting the Meaning Based on Reversal When performing a reading, check the reversed property to select either the upright or reversed meaning for each card. This allows you to interpret the card according to its orientation in the spread.

Summary

  • Custom Deck: You define cards with both upright and reversed meanings.
  • Reversals: Use a function to assign a reversed property to each card.
  • Reading: Select meanings dynamically based on whether the card is drawn in an upright or reversed position.
  • Immutability: The original deck remains unchanged, and reversals are added on the fly during readings.

Full Example Code

import Tarot from "https://esm.sh/gh/MarketingPipeline/Tarot.js";

// Initialize a Tarot instance
const tarot = new Tarot();

// Initialize your custom deck with meanings for both upright and reversed
const your_own_custom_deck = tarot.initializeDeck([
  {
    name: "The Fool",
    meanings: {
      upright: "New beginnings, innocence",
      reversed: "Recklessness, naivety"
    }
  },
  {
    name: "The Coder",
    meanings: {
      upright: "Tired, broke, curious",
      reversed: "Burnout, blocked creativity, frustration"
    }
  },
  {
    name: "The Magician",
    meanings: {
      upright: "Skill, resourcefulness, power",
      reversed: "Manipulation, deceit, illusion"
    }
  },
  {
    name: "The High Priestess",
    meanings: {
      upright: "Wisdom, intuition, mystery",
      reversed: "Secrets, disconnection from intuition"
    }
  }
]);

// Add your custom spread
tarot.addSpread("shakespeare", {
  positions: ["To be", "Not to be"],
  description: "Random example spread"
});

// Function to assign reversals
function assignReversals(deck) {
  return deck.map(card => {
    const isReversed = Math.random() > 0.5;  // 50% chance to reverse the card
    return {
      ...card.card,  // Copy the card object
      reversed: isReversed  // Add `reversed` property
    };
  });
}

// Add reversals to reading
const deckWithReversals = assignReversals(tarot.doReading("shakespeare"));

// Log the result with reversals
console.log(deckWithReversals);

Clone this wiki locally