Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sb-util

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sb-util - npm Package Compare versions

Comparing version 0.0.1-prealpha.3 to 0.0.1-prealpha.4

dist/sb3-serialize.d.ts

75

dist/abstracts.d.ts

@@ -10,2 +10,50 @@ export interface Queryable {

}
export declare enum SB3SerializedInputType {
INPUT_SAME_BLOCK_SHADOW = 1,
INPUT_BLOCK_NO_SHADOW = 2,
INPUT_DIFF_BLOCK_SHADOW = 3
}
export declare enum SB3SerializedBlockType {
MATH_NUM_PRIMITIVE = 4,
POSITIVE_NUM_PRIMITIVE = 5,
WHOLE_NUM_PRIMITIVE = 6,
INTEGER_NUM_PRIMITIVE = 7,
ANGLE_NUM_PRIMITIVE = 8,
COLOR_PICKER_PRIMITIVE = 9,
TEXT_PRIMITIVE = 10,
BROADCAST_PRIMITIVE = 11,
VAR_PRIMITIVE = 12,
LIST_PRIMITIVE = 13
}
export declare enum ScratchVariableTypes {
SCALAR_TYPE = "",
LIST_TYPE = "list",
BROADCAST_MESSAGE_TYPE = "broadcast_msg"
}
export declare type SB3SerializedField = [any, string?];
export interface SB3SerializedFields {
[name: string]: SB3SerializedField;
}
export declare type SB3SerializedInput = [SB3SerializedInputType, SB3SerializedBlockArray | string, (SB3SerializedBlockArray | string)?];
export interface SB3SerializedInputs {
[name: string]: SB3SerializedInput;
}
export interface SB3SharedBlockProperties {
opcode: string;
parent: string;
next: string;
shadow: boolean;
topLevel?: boolean;
x?: number;
y?: number;
}
export interface SB3SerializedBlockObject extends SB3SharedBlockProperties {
inputs: SB3SerializedInputs;
fields: SB3SerializedFields;
}
export declare type SB3SerializedBlockArray = [SB3SerializedBlockType, ...any[]];
export declare type SB3SerializedBlock = SB3SerializedBlockObject | SB3SerializedBlockArray;
export interface SB3SerializedBlocks {
[id: string]: SB3SerializedBlock;
}
export interface SpriteProperties {

@@ -39,11 +87,22 @@ isStage: boolean;

}
export interface BlockProperties {
export interface BlockField {
name: string;
value: any;
id?: string;
variableType?: ScratchVariableTypes;
}
export interface BlockFields {
[name: string]: BlockField;
}
export interface BlockInput {
name: string;
block: string;
shadow: string;
}
export interface BlockProperties extends SB3SharedBlockProperties {
id: string;
opcode: string;
parent: string;
next: string;
inputs: object;
fields: object;
shadow: boolean;
topLevel: boolean;
inputs: {
[name: string]: BlockInput;
};
fields: BlockFields;
}

@@ -50,0 +109,0 @@ export interface BlockQueryProperties {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Serialized Block Interfaces - We should maybe move the serialization typedefs some other file?
var SB3SerializedInputType;
(function (SB3SerializedInputType) {
SB3SerializedInputType[SB3SerializedInputType["INPUT_SAME_BLOCK_SHADOW"] = 1] = "INPUT_SAME_BLOCK_SHADOW";
SB3SerializedInputType[SB3SerializedInputType["INPUT_BLOCK_NO_SHADOW"] = 2] = "INPUT_BLOCK_NO_SHADOW";
SB3SerializedInputType[SB3SerializedInputType["INPUT_DIFF_BLOCK_SHADOW"] = 3] = "INPUT_DIFF_BLOCK_SHADOW";
})(SB3SerializedInputType = exports.SB3SerializedInputType || (exports.SB3SerializedInputType = {}));
var SB3SerializedBlockType;
(function (SB3SerializedBlockType) {
SB3SerializedBlockType[SB3SerializedBlockType["MATH_NUM_PRIMITIVE"] = 4] = "MATH_NUM_PRIMITIVE";
SB3SerializedBlockType[SB3SerializedBlockType["POSITIVE_NUM_PRIMITIVE"] = 5] = "POSITIVE_NUM_PRIMITIVE";
SB3SerializedBlockType[SB3SerializedBlockType["WHOLE_NUM_PRIMITIVE"] = 6] = "WHOLE_NUM_PRIMITIVE";
SB3SerializedBlockType[SB3SerializedBlockType["INTEGER_NUM_PRIMITIVE"] = 7] = "INTEGER_NUM_PRIMITIVE";
SB3SerializedBlockType[SB3SerializedBlockType["ANGLE_NUM_PRIMITIVE"] = 8] = "ANGLE_NUM_PRIMITIVE";
SB3SerializedBlockType[SB3SerializedBlockType["COLOR_PICKER_PRIMITIVE"] = 9] = "COLOR_PICKER_PRIMITIVE";
SB3SerializedBlockType[SB3SerializedBlockType["TEXT_PRIMITIVE"] = 10] = "TEXT_PRIMITIVE";
SB3SerializedBlockType[SB3SerializedBlockType["BROADCAST_PRIMITIVE"] = 11] = "BROADCAST_PRIMITIVE";
SB3SerializedBlockType[SB3SerializedBlockType["VAR_PRIMITIVE"] = 12] = "VAR_PRIMITIVE";
SB3SerializedBlockType[SB3SerializedBlockType["LIST_PRIMITIVE"] = 13] = "LIST_PRIMITIVE";
})(SB3SerializedBlockType = exports.SB3SerializedBlockType || (exports.SB3SerializedBlockType = {}));
var ScratchVariableTypes;
(function (ScratchVariableTypes) {
ScratchVariableTypes["SCALAR_TYPE"] = "";
ScratchVariableTypes["LIST_TYPE"] = "list";
ScratchVariableTypes["BROADCAST_MESSAGE_TYPE"] = "broadcast_msg";
})(ScratchVariableTypes = exports.ScratchVariableTypes || (exports.ScratchVariableTypes = {}));
//# sourceMappingURL=abstracts.js.map

2

dist/block-shapes.d.ts

@@ -1,2 +0,2 @@

declare enum BlockShapes {
export declare enum BlockShapes {
HAT = "hat",

@@ -3,0 +3,0 @@ BOOLEAN = "boolean",

@@ -12,3 +12,3 @@ "use strict";

BlockShapes["CUSTOM"] = "custom";
})(BlockShapes || (BlockShapes = {}));
})(BlockShapes = exports.BlockShapes || (exports.BlockShapes = {}));
var BlockOpcodes;

@@ -15,0 +15,0 @@ (function (BlockOpcodes) {

export declare function map<T, O>(iter: Iterable<T>, predicate: (item: T) => O): Iterator<O>;
export declare function flatmap<T, O>(iter: Iterable<T>, predicate: (item: T) => Iterable<O>): Iterator<O>;
export declare function filter<T>(iter: Iterable<T>, predicate: (item: T) => boolean): Iterator<T>;
export declare function makeIterable<T, U>(iter: Iterable<T>, predicate: (item: Iterable<T>) => Iterator<U>): Iterable<U>;
export declare function makeIterable<T, U>(iter: T, predicate: (item: T) => Iterator<U>): Iterable<U>;
export declare function chain<T>(...iterables: Iterable<T>[]): Iterator<T>;
export declare function first<T>(iter: Iterable<T>): T;
export declare function last<T>(iter: Iterable<T>): T;

@@ -9,2 +9,8 @@ "use strict";

exports.map = map;
function* flatmap(iter, predicate) {
for (const item of iter) {
yield* predicate(item);
}
}
exports.flatmap = flatmap;
function* filter(iter, predicate) {

@@ -11,0 +17,0 @@ for (const item of iter) {

import { Queryable, SpriteProperties, SpritePosition, BlockProperties, SB3ProjectJSON } from './abstracts';
export { BlockShapes } from './block-shapes';
export declare class ScratchProject {

@@ -25,3 +26,3 @@ /**

position(): SpritePosition;
blocks(): BlockCollection;
blocks(query?: string): BlockCollection;
broadcasts(): Record<string, string>;

@@ -33,4 +34,6 @@ lists(): Record<string, any>;

propsIterable(): Iterable<BlockProperties>;
top(): BlockCollection;
first(): Block;
props(): BlockProperties;
byId(id: string): Block;
query(selector: string): BlockCollection;

@@ -37,0 +40,0 @@ [Symbol.iterator](): Iterator<Block>;

@@ -7,2 +7,5 @@ "use strict";

const block_shapes_1 = require("./block-shapes");
var block_shapes_2 = require("./block-shapes");
exports.BlockShapes = block_shapes_2.BlockShapes;
const sb3_serialize_1 = require("./sb3-serialize");
var SpriteAttributes;

@@ -134,8 +137,8 @@ (function (SpriteAttributes) {

}
blocks() {
// DISABLING ESLINT: the blocks in a sprite are an object with many things nested inside
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const blocksObj = this.prop('blocks');
const allBlocks = Object.entries(blocksObj).map(([blockId, block]) => (Object.assign({ id: blockId }, block)));
return new BlockCollection(allBlocks);
blocks(query) {
const blocks = new BlockCollection(generators_1.makeIterable(this.prop('blocks'), sb3_serialize_1.deserializeBlocks));
if (query !== undefined) {
return blocks.query(query);
}
return blocks;
}

@@ -161,2 +164,5 @@ broadcasts() {

}
top() {
return new BlockCollection(generators_1.makeIterable(this.propsIterable(), iter => generators_1.filter(iter, ({ topLevel }) => topLevel)));
}
first() {

@@ -169,2 +175,11 @@ return generators_1.first(generators_1.makeIterable(this, () => this[Symbol.iterator]()));

}
// todo: eventually this should be able to be written as return this.query(`[id="${id}"]`).first()
byId(id) {
for (const block of this) {
if (block.prop('id') === id) {
return block;
}
}
return null;
}
query(selector) {

@@ -171,0 +186,0 @@ const { attr, queryValues: { type, shape, opcode }, } = selector_parse_1.parseBlockQuerySelector(selector);

{
"name": "sb-util",
"version": "0.0.1-prealpha.3",
"version": "0.0.1-prealpha.4",
"description": "Scratch blocks utilities: open and query Scratch blocks formats",

@@ -10,4 +10,4 @@ "repository": "https://github.com/bocoup/sb-util",

"jest": "jest",
"lint": "eslint \"src/**/*.ts\"",
"prettier": "prettier \"src/**/*.ts\"",
"lint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
"prettier": "prettier \"src/**/*.ts\" \"tests/**/*.ts\"",
"test": "npm run prettier -- --check && npm run lint && npm run jest",

@@ -14,0 +14,0 @@ "prepublish": "npm run build"

@@ -13,2 +13,66 @@ export interface Queryable {

// Serialized Block Interfaces - We should maybe move the serialization typedefs some other file?
export enum SB3SerializedInputType {
INPUT_SAME_BLOCK_SHADOW = 1,
INPUT_BLOCK_NO_SHADOW = 2,
INPUT_DIFF_BLOCK_SHADOW = 3,
}
export enum SB3SerializedBlockType {
MATH_NUM_PRIMITIVE = 4,
POSITIVE_NUM_PRIMITIVE = 5,
WHOLE_NUM_PRIMITIVE = 6,
INTEGER_NUM_PRIMITIVE = 7,
ANGLE_NUM_PRIMITIVE = 8,
COLOR_PICKER_PRIMITIVE = 9,
TEXT_PRIMITIVE = 10,
BROADCAST_PRIMITIVE = 11,
VAR_PRIMITIVE = 12,
LIST_PRIMITIVE = 13,
}
export enum ScratchVariableTypes {
SCALAR_TYPE = '',
LIST_TYPE = 'list',
BROADCAST_MESSAGE_TYPE = 'broadcast_msg',
}
export type SB3SerializedField = [any, string?];
export interface SB3SerializedFields {
[name: string]: SB3SerializedField;
}
export type SB3SerializedInput = [
SB3SerializedInputType,
SB3SerializedBlockArray | string,
(SB3SerializedBlockArray | string)?,
];
export interface SB3SerializedInputs {
[name: string]: SB3SerializedInput;
}
export interface SB3SharedBlockProperties {
opcode: string;
parent: string;
next: string;
shadow: boolean;
topLevel?: boolean;
x?: number;
y?: number;
}
export interface SB3SerializedBlockObject extends SB3SharedBlockProperties {
inputs: SB3SerializedInputs;
fields: SB3SerializedFields;
}
export type SB3SerializedBlockArray = [SB3SerializedBlockType, ...any[]];
export type SB3SerializedBlock = SB3SerializedBlockObject | SB3SerializedBlockArray;
export interface SB3SerializedBlocks {
[id: string]: SB3SerializedBlock;
}
export interface SpriteProperties {

@@ -44,11 +108,25 @@ isStage: boolean;

export interface BlockProperties {
export interface BlockField {
name: string;
value: any;
id?: string;
variableType?: ScratchVariableTypes;
}
export interface BlockFields {
[name: string]: BlockField;
}
export interface BlockInput {
name: string;
block: string;
shadow: string;
}
export interface BlockProperties extends SB3SharedBlockProperties {
id: string;
opcode: string;
parent: string;
next: string;
inputs: object;
fields: object;
shadow: boolean;
topLevel: boolean;
inputs: {
[name: string]: BlockInput;
};
fields: BlockFields;
}

@@ -55,0 +133,0 @@

@@ -1,2 +0,2 @@

enum BlockShapes { // information about each block shape can be found here: https://en.scratch-wiki.info/wiki/Blocks#Block_Shapes
export enum BlockShapes { // information about each block shape can be found here: https://en.scratch-wiki.info/wiki/Blocks#Block_Shapes
HAT = 'hat',

@@ -3,0 +3,0 @@ BOOLEAN = 'boolean',

@@ -7,2 +7,8 @@ export function* map<T, O>(iter: Iterable<T>, predicate: (item: T) => O): Iterator<O> {

export function* flatmap<T, O>(iter: Iterable<T>, predicate: (item: T) => Iterable<O>): Iterator<O> {
for (const item of iter) {
yield* predicate(item);
}
}
export function* filter<T>(iter: Iterable<T>, predicate: (item: T) => boolean): Iterator<T> {

@@ -16,6 +22,3 @@ for (const item of iter) {

export function makeIterable<T, U>(
iter: Iterable<T>,
predicate: (item: Iterable<T>) => Iterator<U>,
): Iterable<U> {
export function makeIterable<T, U>(iter: T, predicate: (item: T) => Iterator<U>): Iterable<U> {
return {

@@ -22,0 +25,0 @@ [Symbol.iterator](): Iterator<U> {

@@ -22,2 +22,4 @@ import {

import { BlockOpcodeToShape } from './block-shapes';
export { BlockShapes } from './block-shapes';
import { deserializeBlocks } from './sb3-serialize';

@@ -181,13 +183,8 @@ enum SpriteAttributes {

public blocks(): BlockCollection {
// DISABLING ESLINT: the blocks in a sprite are an object with many things nested inside
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const blocksObj: Record<string, any> = this.prop('blocks');
const allBlocks: Iterable<BlockProperties> = Object.entries(blocksObj).map(
([blockId, block]): BlockProperties => ({
id: blockId,
...block,
}),
);
return new BlockCollection(allBlocks);
public blocks(query?: string): BlockCollection {
const blocks = new BlockCollection(makeIterable(this.prop('blocks'), deserializeBlocks));
if (query !== undefined) {
return blocks.query(query);
}
return blocks;
}

@@ -217,2 +214,8 @@

public top(): BlockCollection {
return new BlockCollection(
makeIterable(this.propsIterable(), iter => filter(iter, ({ topLevel }) => topLevel)),
);
}
public first(): Block {

@@ -227,2 +230,12 @@ return first(makeIterable(this, (): Iterator<Block> => this[Symbol.iterator]()));

// todo: eventually this should be able to be written as return this.query(`[id="${id}"]`).first()
public byId(id: string): Block {
for (const block of this) {
if (block.prop('id') === id) {
return block;
}
}
return null;
}
public query(selector: string): BlockCollection {

@@ -229,0 +242,0 @@ const {

@@ -1,2 +0,13 @@

import { loadSb3, loadProjectJson, loadCloudId, ScratchProject, SpriteCollection, Sprite, BlockCollection, Block } from '../src/sb-util';
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import {
loadSb3,
loadProjectJson,
loadCloudId,
ScratchProject,
SpriteCollection,
Sprite,
BlockCollection,
Block,
} from '../src/sb-util';
import process from 'process';

@@ -7,113 +18,112 @@ import { BlockProperties } from '../src/abstracts';

describe('ScratchProject class --------------------', () => {
describe('loading functions', () => {
let sb3, projectJson, cloudJson;
describe('loading functions', () => {
let sb3, projectJson, cloudJson;
beforeAll(async () => {
sb3 = await loadSb3(`${process.cwd()}/tests/data/test.sb3`);
projectJson = await loadProjectJson(`${process.cwd()}/tests/data/project.json`);
cloudJson = await loadCloudId(319383115);
});
beforeAll(async () => {
sb3 = await loadSb3(`${process.cwd()}/tests/data/test.sb3`);
projectJson = await loadProjectJson(`${process.cwd()}/tests/data/project.json`);
cloudJson = await loadCloudId(319383115);
});
test('is intialized with sb3 file', () => {
expect(sb3).toBeInstanceOf(ScratchProject);
});
test('is intialized with sb3 file', () => {
expect(sb3).toBeInstanceOf(ScratchProject);
});
test('is intialized with project.json file', async () => {
expect(projectJson).toBeInstanceOf(ScratchProject);
});
test('is intialized with project.json file', async () => {
expect(projectJson).toBeInstanceOf(ScratchProject);
});
test('is intialized with cloud ID', async () => {
expect(cloudJson).toBeInstanceOf(ScratchProject);
});
});
test('is intialized with cloud ID', async () => {
expect(cloudJson).toBeInstanceOf(ScratchProject);
});
});
describe('class functions', () => {
let sp, sprites;
beforeAll(async () => {
sp = await loadProjectJson(`${process.cwd()}/tests/data/project.json`);
});
describe('class functions', () => {
let sp, sprites;
test('props returns value for first sprite in SpriteCollection', () => {
expect(sp.sprites().prop('isStage')).toBeDefined();
});
beforeAll(async () => {
sp = await loadProjectJson(`${process.cwd()}/tests/data/project.json`);
});
describe('sprites()', () => {
test('to be returned from a ScratchProject', async () => {
sprites = sp.sprites();
expect(sprites).toBeInstanceOf(SpriteCollection);
});
test('to fail with empty string query string', () => {
expect(() => sp.sprites('')).toThrowError();
});
test('to fail with invalid query string', () => {
expect(() => sp.sprites('[')).toThrowError();
});
test('props returns value for first sprite in SpriteCollection', () => {
expect(sp.sprites().prop('isStage')).toBeDefined();
});
test('to fail with non-string query string', () => {
expect(() => sp.sprites(12345)).toThrowError();
});
test('to query stage with ScratchProject', () => {
const stage = sp.sprites('[isStage=true]');
expect(stage).toBeInstanceOf(SpriteCollection);
});
test('to get stage with stage() function', () => {
const stage = sp.stage();
expect(stage).toBeInstanceOf(Sprite);
const isStageProperty = stage.prop('isStage');
expect(isStageProperty).toBeTruthy();
});
describe('sprites()', () => {
test('to be returned from a ScratchProject', async () => {
sprites = sp.sprites();
expect(sprites).toBeInstanceOf(SpriteCollection);
});
test('to get prop that does not exist', () => {
const stage = sp.stage();
expect(stage.prop('doesNotExist')).toBeUndefined();
});
test('to fail with empty string query string', () => {
expect(() => sp.sprites('')).toThrowError();
});
test('to get prop when sprites are empty', () => {
const emptySprites = sp.sprites('[layerOrder=200]');
expect(emptySprites.prop('isStage')).toBeNull();
});
test('to query for sprites with an attribute that is numeric', () => {
const sprites = sp.sprites('[layerOrder=1]');
expect(sprites).toBeInstanceOf(SpriteCollection);
});
test('to query for sprites with single attribute with ScratchProject', () => {
const sprites = sp.sprites('[x]');
expect(sprites).toBeInstanceOf(SpriteCollection);
});
test('to fail with invalid query string', () => {
expect(() => sp.sprites('[')).toThrowError();
});
test('to query sprites where value is a string with double quotes', () => {
const sprite = sp.sprites('[name="Sprite1"]');;
expect(sprite).toBeInstanceOf(SpriteCollection);
expect(sprite.prop('name')).toEqual('Sprite1');
});
test('to fail with non-string query string', () => {
expect(() => sp.sprites(12345)).toThrowError();
});
test('to query sprites where value is a string with single quotes', () => {
const sprite = sp.sprites("[name='Sprite1']");;
expect(sprite).toBeInstanceOf(SpriteCollection);
expect(sprite.prop('name')).toEqual('Sprite1');
});
});
test('to query stage with ScratchProject', () => {
const stage = sp.sprites('[isStage=true]');
expect(stage).toBeInstanceOf(SpriteCollection);
});
describe('blocks()', () => {
let sp;
beforeAll(async () => {
sp = await loadProjectJson(`${process.cwd()}/tests/data/accelerator.json`);
});
test('to get stage with stage() function', () => {
const stage = sp.stage();
expect(stage).toBeInstanceOf(Sprite);
test('get all blocks in a project', () => {
const blocks = sp.blocks();
expect(blocks).toBeInstanceOf(BlockCollection);
});
});
const isStageProperty = stage.prop('isStage');
expect(isStageProperty).toBeTruthy();
});
});
test('to get prop that does not exist', () => {
const stage = sp.stage();
expect(stage.prop('doesNotExist')).toBeUndefined();
});
test('to get prop when sprites are empty', () => {
const emptySprites = sp.sprites('[layerOrder=200]');
expect(emptySprites.prop('isStage')).toBeNull();
});
test('to query for sprites with an attribute that is numeric', () => {
const sprites = sp.sprites('[layerOrder=1]');
expect(sprites).toBeInstanceOf(SpriteCollection);
});
test('to query for sprites with single attribute with ScratchProject', () => {
const sprites = sp.sprites('[x]');
expect(sprites).toBeInstanceOf(SpriteCollection);
});
test('to query sprites where value is a string with double quotes', () => {
const sprite = sp.sprites('[name="Sprite1"]');
expect(sprite).toBeInstanceOf(SpriteCollection);
expect(sprite.prop('name')).toEqual('Sprite1');
});
test('to query sprites where value is a string with single quotes', () => {
const sprite = sp.sprites("[name='Sprite1']");
expect(sprite).toBeInstanceOf(SpriteCollection);
expect(sprite.prop('name')).toEqual('Sprite1');
});
});
describe('blocks()', () => {
let sp;
beforeAll(async () => {
sp = await loadProjectJson(`${process.cwd()}/tests/data/accelerator.json`);
});
test('get all blocks in a project', () => {
const blocks = sp.blocks();
expect(blocks).toBeInstanceOf(BlockCollection);
});
});
});
});

@@ -126,103 +136,123 @@

describe('Sprite class -------------------------', () => {
let sp, sprite;
let sp, sprite;
beforeAll(async () => {
sp = await loadProjectJson(`${process.cwd()}/tests/data/project.json`);
sprite = sp.sprites('[name="Sprite1"]').first(); // know the sprite's name because of knowledge of project.json file
});
beforeAll(async () => {
sp = await loadProjectJson(`${process.cwd()}/tests/data/project.json`);
sprite = sp.sprites('[name="Sprite1"]').first(); // know the sprite's name because of knowledge of project.json file
});
test('can get sprite position', () => {
const position = sprite.position();
expect(position).toHaveProperty('x');
expect(position).toHaveProperty('y');
});
test('can get sprite position', () => {
const position = sprite.position();
expect(position).toHaveProperty('x');
expect(position).toHaveProperty('y');
});
test('can get broadcasts', () => {
const broadcasts = sprite.broadcasts();
expect(broadcasts).toBeInstanceOf(Object);
});
test('can get broadcasts', () => {
const broadcasts = sprite.broadcasts();
expect(broadcasts).toBeInstanceOf(Object);
});
test('can get lists', () => {
const lists = sprite.lists();
expect(lists).toBeInstanceOf(Object);
});
test('can get lists', () => {
const lists = sprite.lists();
expect(lists).toBeInstanceOf(Object);
});
test('can get all blocks', () => {
const blocks = sprite.blocks();
expect(blocks).toBeInstanceOf(BlockCollection);
});
test('can get all blocks', () => {
const blocks = sprite.blocks();
expect(blocks).toBeInstanceOf(BlockCollection);
});
});
describe('BlockCollection class ----------------', () => {
let sp, blocks;
let sp, blocks;
beforeAll(async () => {
// Choosing this file because it has a lot of blocks
sp = await loadProjectJson(`${process.cwd()}/tests/data/accelerator.json`);
blocks = sp.blocks();
});
beforeAll(async () => {
// Choosing this file because it has a lot of blocks
sp = await loadProjectJson(`${process.cwd()}/tests/data/accelerator.json`);
blocks = sp.blocks();
});
test('query for opcode', () => {
const eventFlagBlocks = blocks.query('event_whenflagclicked');
for (const block of eventFlagBlocks) {
expect(block.prop('opcode')).toEqual('event_whenflagclicked');
}
});
test('query for opcode of serialized block', () => {
expect.assertions(4);
const eventFlagBlocks = blocks.query('event_whenflagclicked');
for (const block of eventFlagBlocks) {
expect(block.prop('opcode')).toEqual('event_whenflagclicked');
}
});
test('query for opcode not present', () => {
const invalidOpcodeBlocks = blocks.query('some_opcode');
expect(invalidOpcodeBlocks.first()).toBeNull();
});
test('query for opcode of deserialized block', () => {
expect.assertions(65);
for (const block of blocks.query('math_number')) {
expect(block.prop('opcode')).toEqual('math_number');
}
});
test('query for block type', () => {
const motionBlocks = blocks.query('.motion');
const first = motionBlocks.first();
expect(first).not.toBeNull();
expect(first).toBeInstanceOf(Block);
expect(first.prop('opcode')).toContain('motion');
});
test('query for opcode not present', () => {
const invalidOpcodeBlocks = blocks.query('some_opcode');
expect(invalidOpcodeBlocks.first()).toBeNull();
});
test('query for invalid block type', () => {
const invalidTypeBlocks = blocks.query(':invalid');
expect(invalidTypeBlocks.first()).toBeNull();
});
test('query for block type', () => {
const motionBlocks = blocks.query('.motion');
const first = motionBlocks.first();
expect(first).not.toBeNull();
expect(first).toBeInstanceOf(Block);
expect(first.prop('opcode')).toContain('motion');
});
test('query for block shape', () => {
const reporterBlocks = blocks.query(':reporter');
expect(BlockOpcodeToShape[reporterBlocks.first().prop('opcode')]).toEqual('reporter');
});
test('query for invalid block type', () => {
const invalidTypeBlocks = blocks.query(':invalid');
expect(invalidTypeBlocks.first()).toBeNull();
});
test('query for block type and shape', () => {
const reporterBlocks = blocks.query('.operator :reporter');
const first = reporterBlocks.first();
expect(first.prop('opcode')).toContain('operator');
expect(BlockOpcodeToShape[first.prop('opcode')]).toEqual('reporter');
});
test('query for block shape', () => {
const reporterBlocks = blocks.query(':reporter');
expect(BlockOpcodeToShape[reporterBlocks.first().prop('opcode')]).toEqual('reporter');
});
test('query for block type and shape, order does not matter', () => {
const reporterBlocks = blocks.query(':reporter .operator');
const first = reporterBlocks.first();
expect(first.prop('opcode')).toContain('operator');
expect(BlockOpcodeToShape[first.prop('opcode')]).toEqual('reporter');
});
test('query for block type and shape', () => {
const reporterBlocks = blocks.query('.operator :reporter');
const first = reporterBlocks.first();
expect(first.prop('opcode')).toContain('operator');
expect(BlockOpcodeToShape[first.prop('opcode')]).toEqual('reporter');
});
test('query for block type and shape, order does not matter', () => {
const reporterBlocks = blocks.query(':reporter .operator');
const first = reporterBlocks.first();
expect(first.prop('opcode')).toContain('operator');
expect(BlockOpcodeToShape[first.prop('opcode')]).toEqual('reporter');
});
});
describe('Block class -------------------------', () => {
let sp, blocks;
let sp, blocks;
beforeAll(async () => {
// Choosing this file because it has a lot of blocks
sp = await loadProjectJson(`${process.cwd()}/tests/data/accelerator.json`);
blocks = sp.blocks();
});
beforeAll(async () => {
// Choosing this file because it has a lot of blocks
sp = await loadProjectJson(`${process.cwd()}/tests/data/accelerator.json`);
blocks = sp.blocks();
});
test('the first block exists', () => {
expect(blocks.first()).toBeInstanceOf(Block);
});
test('the first block exists', () => {
expect(blocks.first()).toBeInstanceOf(Block);
});
test('get all properties of a block as an object', () => {
const blockProps: BlockProperties = blocks.first().props();
expect(blockProps).toBeInstanceOf(Object);
expect(blockProps).toHaveProperty('id');
});
test('can find top blocks', () => {
expect.assertions(10);
for (const block of blocks.top()) {
expect(block.prop('topLevel')).toBeTruthy();
}
});
test('get all properties of a block as an object', () => {
const blockProps: BlockProperties = blocks.first().props();
expect(blockProps).toBeInstanceOf(Object);
expect(blockProps).toHaveProperty('id');
});
test('can find block by id', () => {
const block = blocks.byId('8RrT-%*Y/x{|LPTvspp_');
expect(block.prop('opcode')).toEqual('motion_changexby');
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc