Skip to content

Instantly share code, notes, and snippets.

@lyatziv
Created March 17, 2022 23:00
Show Gist options
  • Save lyatziv/c0c2328823dc0523e5dd1edde7bbc7f9 to your computer and use it in GitHub Desktop.
Save lyatziv/c0c2328823dc0523e5dd1edde7bbc7f9 to your computer and use it in GitHub Desktop.
Get page numbering for booklet printing
const makePamphlet = (suspectInput) => {
const input = Boolean(suspectInput % 2) ? suspectInput + 1 : suspectInput;
const evenPages = [
...Array(input / 2)
.fill(0)
.map((_el, idx) => (idx + 1) * 2),
].reverse();
const oddPages = [
...Array(input)
.fill(0)
.map((_el, idx) => idx % 2 && idx)
.filter((e) => e),
];
return [
...Array(input/2)
.fill(0)
.map((_el, idx) => {
const output = (Boolean(idx%2))
? [`${evenPages.pop()}`, `${oddPages.pop()}`] // second [2, 15]
: [`${evenPages.shift()}`, `${oddPages.shift()}`] // first [16,1]
return output
})
].flatMap(p => p).map(n => parseInt(n));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment