Public
Edited
Jun 17, 2023
Insert cell
Insert cell
Insert cell
function encode(obj) {
const $my = Symbol('my');
const $open = Symbol("open");
const $close = Symbol("close");
const $is = Symbol("is");
const $are = Symbol("are");
const $done = Symbol("done");

const gen = helper(obj, 0);
let _peek = null;
try {
for (;;) {
//
}
//
} catch (e) {
if (e !== $done) {
throw e;
}
}

function peek() {
if (_peek === null) {
_peek = pop();
}
return _peek;
}

function pop() {
if (_peek !== null) {
const temp = _peek;
_peek = null;
return temp;
}

const { value, done } = gen.next();
if (done) {
throw $done;
}

return value;
}

function* helper(obj, depth) {
for (const [key, value] of Object.entries(obj)) {
if (depth === 0) yield "My";
yield key;

if (typeof value === 'object') {
yield $is;
yield $open;

yield* helper(value, depth+1);

} else if (Array.isArray(value)) {
yield $are;
yield $open;

for (const item of value) {
if (typeof item === 'object') {
yield* helper(item, depth+1);
}
}

yield $close;

} else {
yield $is;
yield value;
}
}
}
}
Insert cell
toEnglish({ foo: 'bar', baz: 'bing' })
Insert cell
function toEnglish(obj) {
const parts = [];
for (const part of o(obj)) {
// need to join with context-sensitive whitespace.
// Don't add a space after a word if the next word is punctuation.
if (parts.length <= 0) {
parts.push(part);
continue;
}

if (!":,;.?".includes(part.charAt(0))) {
parts.push(" ");
}
parts.push(part);
}

return parts; //.join('');

function kind(thing) {
if (Array.isArray(thing)) return "a";
else if (typeof thing === "object") return "o";
else return null;
}

function* o(obj) {
for (const [key, value] of Object.entries(obj)) {
yield "My";
yield key;

if (kind(value) === "a") {
yield "are:";
yield* oa(value);
//
} else if (kind(value) === "o") {
yield "is:";
yield* oo(value);
//
} else {
yield "is";
yield value;
}

yield ".";
}
}

function* oo(obj) {
}

function* oa(arr) {}

function* oao(obj) {}
}
Insert cell
foo({name:"John Doe",age:30,isStudent:false,hobbies:["reading","running","painting"],address:{street:"123 Main St",city:"New York",zip:"10001"},friends:[{name:"Jane Smith",age:28},{name:"David Johnson",age:32}]})
Insert cell
function foo(obj) {
return Array.from(itertree(obj));

function* itertree(obj, typestring='', ...keys) {
let cur = obj;
for (const key of keys) {
cur = cur[key];
}

typestring = typestring + kind(cur);
if (kind(cur) === 'v') {
yield { keys, typestring, value: cur };

} else if (kind(cur) === 'a') {
for (let i=0, n=cur.length; i<n; ++i) {
yield* itertree(obj, typestring, ...keys, i);
}

} else if (kind(cur) === 'o') {
for (const key of Object.keys(cur)) {
yield* itertree(obj, typestring, ...keys, key);
}
}
}

function kind(thing) {
if (Array.isArray(thing)) return "a";
else if (typeof thing === "object") return "o";
else return 'v';
}
}
Insert cell
Insert cell
DATA
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
import { inspect } from '@player1537/utilities'
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more