|
|
|
|
|
|
|
const SOUP = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#%()*+,-./:;=?@[]^_`{|}~'; |
|
const generateId = i => { |
|
let str = ''; |
|
while (i >= 0) { |
|
str = SOUP[i % SOUP.length] + str; |
|
i = Math.floor(i / SOUP.length) - 1; |
|
} |
|
return str; |
|
}; |
|
|
|
class Pool { |
|
constructor () { |
|
this.generatedIds = new Map(); |
|
this.references = new Map(); |
|
this.skippedIds = new Set(); |
|
|
|
|
|
this.skippedIds.add('of'); |
|
} |
|
skip (id) { |
|
this.skippedIds.add(id); |
|
} |
|
addReference (id) { |
|
const currentCount = this.references.get(id) || 0; |
|
this.references.set(id, currentCount + 1); |
|
} |
|
generateNewIds () { |
|
const entries = Array.from(this.references.entries()); |
|
|
|
entries.sort((a, b) => b[1] - a[1]); |
|
|
|
let i = 0; |
|
for (const entry of entries) { |
|
const oldId = entry[0]; |
|
|
|
let newId = generateId(i); |
|
while (this.skippedIds.has(newId)) { |
|
i++; |
|
newId = generateId(i); |
|
} |
|
|
|
this.generatedIds.set(oldId, newId); |
|
i++; |
|
} |
|
} |
|
getNewId (originalId) { |
|
if (this.generatedIds.has(originalId)) { |
|
return this.generatedIds.get(originalId); |
|
} |
|
return originalId; |
|
} |
|
} |
|
|
|
const compress = projectData => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const pool = new Pool(); |
|
|
|
for (const target of projectData.targets) { |
|
|
|
|
|
|
|
for (const variableId of Object.keys(target.variables)) { |
|
pool.skip(variableId); |
|
} |
|
for (const listId of Object.keys(target.lists)) { |
|
pool.skip(listId); |
|
} |
|
for (const broadcastId of Object.keys(target.broadcasts)) { |
|
pool.skip(broadcastId); |
|
} |
|
for (const blockId of Object.keys(target.blocks)) { |
|
const block = target.blocks[blockId]; |
|
pool.addReference(blockId); |
|
if (Array.isArray(block)) { |
|
|
|
continue; |
|
} |
|
if (block.parent) { |
|
pool.addReference(block.parent); |
|
} |
|
if (block.next) { |
|
pool.addReference(block.next); |
|
} |
|
if (block.comment) { |
|
pool.addReference(block.comment); |
|
} |
|
for (const input of Object.values(block.inputs)) { |
|
for (let i = 1; i < input.length; i++) { |
|
const inputValue = input[i]; |
|
if (typeof inputValue === 'string') { |
|
pool.addReference(inputValue); |
|
} |
|
} |
|
} |
|
} |
|
|
|
for (const commentId of Object.keys(target.comments)) { |
|
const comment = target.comments[commentId]; |
|
pool.addReference(commentId); |
|
if (comment.blockId) { |
|
pool.addReference(comment.blockId); |
|
} |
|
} |
|
} |
|
|
|
pool.generateNewIds(); |
|
for (const target of projectData.targets) { |
|
const newBlocks = {}; |
|
const newComments = {}; |
|
for (const blockId of Object.keys(target.blocks)) { |
|
const block = target.blocks[blockId]; |
|
newBlocks[pool.getNewId(blockId)] = block; |
|
if (Array.isArray(block)) { |
|
|
|
continue; |
|
} |
|
if (block.parent) { |
|
block.parent = pool.getNewId(block.parent); |
|
} |
|
if (block.next) { |
|
block.next = pool.getNewId(block.next); |
|
} |
|
if (block.comment) { |
|
block.comment = pool.getNewId(block.comment); |
|
} |
|
for (const input of Object.values(block.inputs)) { |
|
for (let i = 1; i < input.length; i++) { |
|
const inputValue = input[i]; |
|
if (typeof inputValue === 'string') { |
|
input[i] = pool.getNewId(inputValue); |
|
} |
|
} |
|
} |
|
} |
|
|
|
for (const commentId of Object.keys(target.comments)) { |
|
const comment = target.comments[commentId]; |
|
newComments[pool.getNewId(commentId)] = comment; |
|
if (comment.blockId) { |
|
comment.blockId = pool.getNewId(comment.blockId); |
|
} |
|
} |
|
|
|
target.blocks = newBlocks; |
|
target.comments = newComments; |
|
} |
|
}; |
|
|
|
module.exports = compress; |
|
|