### JavaScript Example for Processing Data Source: https://thomasjfrank.com/docs/creators-companion/databases/sponsors This JavaScript code snippet demonstrates a potential use case for processing data, possibly related to content or payments. The specific functionality is not fully detailed in the provided context, but it appears to involve conditional logic and data manipulation. ```javascript // This appears to be a placeholder or incomplete JavaScript snippet. // Further context is needed to determine its exact functionality and purpose. ``` -------------------------------- ### Calculate Next Due Start Date based on Recurrence Rules Source: https://thomasjfrank.com/docs/ultimate-brain/databases/tasks This is a comprehensive snippet that calculates 'nextDueStart' based on various recurrence rules. It handles 'days' recurrence with specific weekdays, 'nthweekday' recurrence, 'monthsonthelastday', 'monthsonthefirstweekday', and 'monthsonthelastweekday'. It checks against 'dateNow' and 'dateDue' to determine the appropriate next date. ```Custom Logic nextDueStart, ifs( recurUnit == "days" and length(weekdays) > 0 and recurIntervalProp == 1, if( dateNow >= dateDue, ifs( includes(weekdays, format(day(dateAdd(dateNow, 1, "days")))), dateAdd(dateNow, 1, "days"), includes(weekdays, format(day(dateAdd(dateNow, 2, "days")))), dateAdd(dateNow, 2, "days"), includes(weekdays, format(day(dateAdd(dateNow, 3, "days")))), dateAdd(dateNow, 3, "days"), includes(weekdays, format(day(dateAdd(dateNow, 4, "days")))), dateAdd(dateNow, 4, "days"), includes(weekdays, format(day(dateAdd(dateNow, 5, "days")))), dateAdd(dateNow, 5, "days"), includes(weekdays, format(day(dateAdd(dateNow, 6, "days")))), dateAdd(dateNow, 6, "days"), includes(weekdays, format(day(dateAdd(dateNow, 7, "days")))), dateAdd(dateNow, 7, "days"), emptyDate ), ifs( includes(weekdays, format(day(dateAdd(dateDue, 1, "days")))), dateAdd(dateDue, 1, "days"), includes(weekdays, format(day(dateAdd(dateDue, 2, "days")))), dateAdd(dateDue, 2, "days"), includes(weekdays, format(day(dateAdd(dateDue, 3, "days")))), dateAdd(dateDue, 3, "days"), includes(weekdays, format(day(dateAdd(dateDue, 4, "days")))), dateAdd(dateDue, 4, "days"), includes(weekdays, format(day(dateAdd(dateDue, 5, "days")))), dateAdd(dateDue, 5, "days"), includes(weekdays, format(day(dateAdd(dateDue, 6, "days")))), dateAdd(dateDue, 6, "days"), includes(weekdays, format(day(dateAdd(dateDue, 7, "days")))), dateAdd(dateDue, 7, "days"), emptyDate ) ), recurUnit == "nthweekday" and length(weekdays) == 1 and recurIntervalProp >= 1 and recurIntervalProp <= 5, if( month(nthWeekdayBaseDate) == month(firstWeekSpecificDayBaseDate), nthWeekdayBaseDate, dateSubtract(nthWeekdayBaseDate, 1, "week") ), recurUnit == "monthsonthelastday", if( dateNow >= lastDayBaseDate, nextLastBaseDate, lastDayBaseDate ), recurUnit == "monthsonthefirstweekday", if( dateNow >= firstWeekdayBaseDate, nextFirstWeekday, firstWeekdayBaseDate ), recurUnit == "monthsonthelastweekday", if( dateNow >= firstLastWeekdayBaseDate, nextLastWeekday, firstLastWeekdayBaseDate ), recurUnit == "month", if( dateNow >= firstWeekSpecificDayBaseDate, nthWeekdayBaseDate, firstWeekSpecificDayBaseDate ), emptyDate ) ``` -------------------------------- ### Generate YouTube Chapters from B-Roll Metadata (Notion Formula) Source: https://thomasjfrank.com/docs/creators-companion/databases/content This Notion formula creates a YouTube chapter list from B-Roll items with a 'Chapter' meta type and their associated timecodes. It expects timecodes in 'HH:MM:SS:FF' format and handles the creation of chapter entries, including a default 'Introduction' if the first timestamp is not at the beginning. ```javascript lets( chapterMarkers, prop("B-Roll").filter(current.prop("Meta").includes("Chapter")).sort(current.prop("Timecode")).map( current.prop("Timecode") .replace("^(.*?):\d{2}$", "$1") .replace("^00:(\d{2}:\d{2})","$1") + " - " + current.prop("Name") ), if( chapterMarkers.first().test("^00:00") or chapterMarkers.empty(), chapterMarkers.join("\n"), "00:00 - Introduction\n" + chapterMarkers.join("\n") ) ) ``` -------------------------------- ### Notion Formula: Pages Read Calculation Source: https://thomasjfrank.com/docs/ultimate-brain/databases/reading-log This JavaScript formula calculates the number of pages read in a session by subtracting the 'Start Page' from the 'End Page'. It ensures both 'Start Page' and 'End Page' properties are not empty before performing the calculation, defaulting to 0 if they are. ```javascript if( not empty(prop("Start Page")) && not empty(prop("End Page")), prop("End Page") - prop("Start Page"), 0 ) ``` -------------------------------- ### Calculate Session Duration in Minutes (JavaScript) Source: https://thomasjfrank.com/docs/ultimate-brain/databases/work-sessions This JavaScript formula calculates the duration of a work session in minutes. It subtracts the 'Start' date/time from the 'End' date/time. If 'End' is empty, it calculates the duration from 'Start' until the current time ('now()'). Ensures the duration is not negative. ```javascript let( duration, if( empty(prop("End")), dateBetween(now(), prop("Start"), "minutes"), dateBetween(prop("End"), prop("Start"), "minutes") ), duration >= 0 ? duration : 0 ) ``` -------------------------------- ### Create Date Range for Calendar View (JavaScript) Source: https://thomasjfrank.com/docs/ultimate-brain/databases/work-sessions This JavaScript formula creates a date range object from the 'Start' and 'End' date properties. This is specifically used as the 'Calendar By' property in a Notion Calendar view to display sessions. ```javascript dateRange( prop("Start"), prop("End") ) ``` -------------------------------- ### Basic String Comparison for Option Names in Notion Formulas Source: https://thomasjfrank.com/docs/ultimate-brain/databases/tasks This JavaScript snippet demonstrates a basic string comparison to check an option in a Notion property. It's a common pattern for simple checks but fails if the option name is changed or localized. This highlights the need for a more robust solution like the Localization Key. ```javascript if( prop("Recur Unit") == "Days(s)", true, false ) ``` -------------------------------- ### Calculate Recipe Count Formula (JavaScript) Source: https://thomasjfrank.com/docs/ultimate-brain/databases/recipe-tags This JavaScript formula calculates the number of recipes associated with a specific tag and formats the output string. It uses Notion's `lets` function for variable assignment and `prop` to access related properties. The output is styled with color and background. ```javascript lets( recipeCount, prop("Recipes").length(), plurality, if( recipeCount == 1, "", "s" ), ( recipeCount.format() + " recipe" + plurality ).style( "c", "b", "blue", "blue_background" ) ) ``` -------------------------------- ### Extract Base URL using Notion Formula Source: https://thomasjfrank.com/docs/ultimate-brain/databases/recipes This Notion formula extracts the base domain from a URL. It removes the protocol and any path segments after the first '/'. It relies on the `prop("URL")` function to access the URL property of the current database item. ```javascript prop("URL") .replace("[^]*//", "") .split("/") .first() ``` -------------------------------- ### Calculate Recurring Dates Source: https://thomasjfrank.com/docs/ultimate-brain/databases/tasks This snippet calculates recurring dates based on a starting date (dateDue), a recurrence interval (recurIntervalProp), and conditional logic involving the current date (conditionalDateNow). It also considers whether to include specific weekdays in the recurrence. ```Formula dateSubtract(dateAdd(dateSubtract(dateAdd(dateDue, ceil((year(conditionalDateNow) * 12 + month(conditionalDateNow) - (year(dateDue) * 12 + month(dateDue))) / recurIntervalProp) * recurIntervalProp, "months"), date(dateAdd(dateDue, ceil((year(conditionalDateNow) * 12 + month(conditionalDateNow) - (year(dateDue) * 12 + month(dateDue))) / recurIntervalProp) * recurIntervalProp, "months")) - 1, "days"), 1, "months"), 1, "days") ``` ```Formula dateSubtract(dateAdd(dateSubtract(dateAdd(dateDue, recurIntervalProp, "months"), date(dateAdd(dateDue, recurIntervalProp, "months")) - 1, "days"), 1, "months"), 1, "days") ``` -------------------------------- ### My Day Label Formula (JavaScript) Source: https://thomasjfrank.com/docs/ultimate-brain/databases/tasks This formula simply outputs the string 'My Day'. It is used to visually label the 'My Day' checkbox on a dedicated page. The 'showLabel' variable controls the output, though it's hardcoded to true in this example. ```javascript let( showLabel, true, if( showLabel, "My Day", "" ) ) ``` -------------------------------- ### Format Recipe Time Card (JavaScript Formula) Source: https://thomasjfrank.com/docs/ultimate-brain/databases/recipes This JavaScript formula calculates the total preparation and cooking time for a recipe and formats it into a human-readable string, styled as a 'card'. It handles pluralization for hours and minutes and provides different formatting based on the total time duration. ```javascript lets( totalTimeRaw, prop("Cook Time") + prop("Prep Time"), hours, floor(totalTimeRaw / 60), minutes, totalTimeRaw % 60, hourPlurality, if( hours == 1, "", "s" ), minutePlurality, if( minutes == 1, "", "s" ), time, ifs( totalTimeRaw > 60, hours + " hr" + hourPlurality + ", " + minutes + " minute" + minutePlurality, totalTimeRaw == 60, hours + " hr", minutes + " min" + minutePlurality ), time.style( "c", "b", "green", "green_background" ) ) ``` -------------------------------- ### Robust Option Comparison Using Localization Key in Notion Formulas Source: https://thomasjfrank.com/docs/ultimate-brain/databases/tasks This JavaScript snippet shows how to perform a more robust comparison using the Localization Key property. It checks the option against both a hardcoded string and a value retrieved from the Localization Key array, ensuring formula integrity even after localization. It utilizes zero-based indexing for array access. ```javascript if( prop("Recur Unit") == "Days(s)" or prop("Recur Unit") == prop("Localization Key").at(1).at(0), true, false ) ``` -------------------------------- ### Get Parent Edited Timestamp in Notion Source: https://thomasjfrank.com/docs/ultimate-brain/databases/tasks This JavaScript formula returns the 'Edited' timestamp of the parent task if the current task is a sub-task. Otherwise, it returns the current task's 'Edited' timestamp. This is used for sorting sub-tasks under their respective parent tasks. ```javascript !prop("Parent Task") ? prop("Edited").timestamp() : prop("Parent Task").first().prop("Edited").timestamp() ``` -------------------------------- ### Localize Notion Project Status Options (JavaScript) Source: https://thomasjfrank.com/docs/creators-companion/databases/projects This JavaScript formula is designed to localize the status options for Notion projects, such as 'Planned', 'On Hold', 'Doing', 'Ongoing', and 'Done'. It allows users to translate these status names and define which options are considered 'Active'. This is crucial for filtering database views correctly, especially when dealing with different languages. ```javascript /* If you would like to translate your Status option names, translate them in the Status property AND in this formula as well. Below, you'll see variables for each of the important Status options in Ultimate Tasks. For each, you can replace the terms in quotes ("") with translated terms. The final argument (the List defined by the brackets) is the return value of this formula. It is a multi-dimensional List (e.g. array), where the most important element is the 2nd (index 1) of the top List. This sub-List contains all of the Status options that are considered Active. Since Notion formulas don't let us access the Groups of Status properties (e.g. To Do, In Progress, Complete), this is the best way to get the Group of a translated (or added) Status option. If you have added extra options that are considered Active, add them as strings between the brackets underneath the additionalActiveStatusOptions variable. This can be helpful if you want to define multiple stages for Active projects. Example: ["Design", "Development", "QA"] */ lets( planned, /* Edit the term below this line to translated the Planned status. E.g. "Zaplanowany" */ "Planned", onHold, /* Edit the term below this line to translated the On Hold status. "Zastopowany" */ "On Hold", doing, /* Edit the term below this line to translated the Doing status. "W trakcie" */ "Doing", ongoing, /* Edit the term below this line to translated the Ongoing status. "Otwarty" */ "Ongoing", done, /* Edit the term below this line to translated the Done status. "Zakończony" */ "Done", additionalActiveStatusOptions, /* If you would like to add additional Active status options, add them as strings between these brackets. See the comment above for an example. */ [], /* Do not edit anything below this line */ [ [planned, onHold], [doing, ongoing, additionalActiveStatusOptions].flat(), [done] ] ) ``` -------------------------------- ### Get Parent Task Due Timestamp in Notion Source: https://thomasjfrank.com/docs/ultimate-brain/databases/tasks This JavaScript formula retrieves the 'Due Timestamp' of a parent task if the current task is a sub-task. Otherwise, it returns the current task's 'Due Timestamp'. This is crucial for hierarchical sorting within project views. ```javascript !prop("Parent Task") ? prop("Due Timestamp") : prop("Parent Task").first().prop("Due Timestamp") ``` -------------------------------- ### Format Statistics Formula Source: https://thomasjfrank.com/docs/creators-companion/databases/content This formula formats view, like, and comment counts into a readable string with commas for thousands separators and applies styling. It handles cases where some or all statistics are empty, ensuring a clean output. It uses regular expressions for comma formatting and custom style functions. ```javascript if( prop("Views").empty() and prop("Likes").empty() and prop("Comments").empty(), "", lets( firstCommaSetter, "(\d+)(\d{3})", secondCommaSetter, "(\d+)(\d{3},\d{3})", views, if( prop("Views").empty(), "", (" " + prop("Views"). replace(firstCommaSetter,"\$1,\$2"). replace(secondCommaSetter,"\$1,\$2") ).style("c","b","green","green_background") ), likes, if( prop("Likes").empty(), "", (" " + prop("Likes"). replace(firstCommaSetter,"\$1,\$2"). replace(secondCommaSetter,"\$1,\$2") ).style("c","b","green","green_background") ), comments, if( prop("Comments").empty(), "", (" " + prop("Comments"). replace(firstCommaSetter,"\$1,\$2"). replace(secondCommaSetter,"\$1,\$2") ).style("c","b","green","green_background") ), [views, likes, comments].filter(!current.empty()).join("\n") ) ) ```