Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
If you're reaaaally lazy you could do your database something like this demo of a blog/forum

https://mongodb-sync.glitch.me/

Source code:

https://glitch.com/edit/#!/mongodb-sync

After finding that one I've even adapted it to make a little programming website that hosts submitted JavaScript classes

MongoDB is a database where you don't have to mess with SQL; you could just store all your key values in a big JSON object turned into a string

Happy Thread fucked around with this message at 22:09 on Mar 14, 2019

Adbot
ADBOT LOVES YOU

necrotic
Aug 2, 2005
I owe my brother big time for this!
I'd definitely recommend Firebase over Mongo, for a couple reasons. One being cost (depending on scale, but this sounds small), another being operational overhead.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
What they did for that demo for MongoDB is they just signed up for a free account on mlab.com, which I did too and can confirm it's easy and easy to configure to store big dictionaries of JSON data. There were barely any steps involved. Their free service is also pretty slow (I think I did a stress test once and it wouldn't move more than like a megabyte per minute, with latency).

necrotic
Aug 2, 2005
I owe my brother big time for this!
Using firebase you can just toss your site up on a static host (including firebase, which has a static site hosting option). The mongo approach requires running your own backend for the mongo access.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

necrotic posted:

Using firebase you can just toss your site up on a static host (including firebase, which has a static site hosting option). The mongo approach requires running your own backend for the mongo access.
The downside of static hosted firebase is that getting the permissions stuff right (so users can't just poke around in your database however they want) is fairly tricky, or at least it was when I last tried. (I see there's built-in login and user-level authentication stuff now so maybe it's better, it's been a while since I tried it.)

Emmideer
Oct 20, 2011

Lovely night, no?
Grimey Drawer
firestore has immediately mind-trapped me into obsessing over minimizing document gets even though realistically the 50k limit isn't anywhere near a problem yet

Emmideer
Oct 20, 2011

Lovely night, no?
Grimey Drawer
This might be overkill because really I'm only looking to read from my database

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Just your friendly reminder that most people wanting to use mongo and the like should really be using a relational database.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I have a relatively very simple library, it has one dependency. I'd like to add one or two simple tests to it but I'd like to do so without requiring loving 200 additional modules. I've looked at every single test lib I can find, is there anything pretty basic and simple out there?

Should I just, essentially, write some asserts into a js file in order to avoid madness?

Chenghiz
Feb 14, 2007

WHITE WHALE
HOLY GRAIL
Yeah, you could just use nodejs assert.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I think I'm happy with this.

JavaScript code:
const assert = require('assert');

function it (description, cb) {
    process.stdout.write(description);
    try {
        cb();
        process.stdout.write(' \x1b[32m\u2713\x1b[0m\n');
    } catch (err) {
        process.stdout.write(' \x1b[31m\u2717\x1b[0m\n');
        throw err;
    }
}

it('should initialise without parameters', () => {
    assert.equal(1, 1);
});
it('should do something else', () => {
    assert.equal(1, 1);
});

Nolgthorn fucked around with this message at 15:41 on Mar 16, 2019

Fruit Smoothies
Mar 28, 2004

The bat with a ZING
JavaScript code:
	async add(func) {
		await Promise.all(this.list);
		let promise = new Promise(async (done,fail) => {
			try {
				await func();
				done();
			} catch(e) {
				fail();
			}
		});
		this.list.push(promise);
	}
Usage is meant to be:

JavaScript code:
obj.add(async () => {
	await thing();
	await otherThing();
});
But they aren't executed in order and I have no idea why.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Testing libraries basic purpose is to make tests easier to run, read, write, and maintain at the cost of learning the testing library's rules and the installation of more modules.

Some of the best unit tests I've seen have been just using the languages native assertion facilities.

Doom Mathematic
Sep 2, 2008

Fruit Smoothies posted:

JavaScript code:
	async add(func) {
		await Promise.all(this.list);
		let promise = new Promise(async (done,fail) => {
			try {
				await func();
				done();
			} catch(e) {
				fail();
			}
		});
		this.list.push(promise);
	}
Usage is meant to be:

JavaScript code:
obj.add(async () => {
	await thing();
	await otherThing();
});
But they aren't executed in order and I have no idea why.

I suggest removing the async from new Promise(async (done,fail) => {. Honestly I don't have a clue what exactly it would do in that context but I'm reasonably sure it's not meant to be there.

Actually I think you might be able to get away with eliminating that whole wrapping Promise and just writing this.list.push(func());?

Fruit Smoothies
Mar 28, 2004

The bat with a ZING

Doom Mathematic posted:

Actually I think you might be able to get away with eliminating that whole wrapping Promise and just writing this.list.push(func());?

That is much simpler, but it doesn't solve the underlying issue sadly. It waits for the first promise, but not subsequent ones.

necrotic
Aug 2, 2005
I owe my brother big time for this!
Is the only way to add to the list from this add method?

Either way, the wrapping promise is redundant. An async function itself is a promise so you're doubling up for no reason. The out of order execution is a different issue.

I am not near a computer to really look at it, but will look later if you havent figured it out.

Fruit Smoothies
Mar 28, 2004

The bat with a ZING
Edit: Stil having issues

necrotic
Aug 2, 2005
I owe my brother big time for this!
Are you calling this as await obj.add or just plain obj.add? The latter won't work here, calling an async function spawns a new promise and if you do not wait on that promise it may not be invoked before the next time you call obj.add.

Fruit Smoothies
Mar 28, 2004

The bat with a ZING

necrotic posted:

Are you calling this as await obj.add or just plain obj.add? The latter won't work here, calling an async function spawns a new promise and if you do not wait on that promise it may not be invoked before the next time you call obj.add.

I'm not awaiting because add() is typically an event-emitted callback. I have given up and I'm now using an npm package called easy-promise-queue. Thanks though

necrotic
Aug 2, 2005
I owe my brother big time for this!

Fruit Smoothies posted:

I'm not awaiting because add() is typically an event-emitted callback. I have given up and I'm now using an npm package called easy-promise-queue. Thanks though

Using a library is probably a good choice. For your learning, this approach works if you can't await the call to add:

code:
class Chain {
  constructor() {
    this.list = [];
    this.all = new Promise(resolve => {
      this.resolve = resolve;
    });
  }

  add(func) {
    const pos = this.list.length;
    this.list.push(async () => {
      await func();
      const next = this.list[pos + 1];

      if (next) {
        next();
      } else {
        this.resolve();
      }
    });

    if (this.list.length === 1) {
      this.list[0]();
    }
  }
}
The all promise is only useful if you ever want to wait for all enqueued work to finish.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Why do JavaScript's ES Modules sort their contents alphabetically when you import them? Why would anyone want that? I'm trying to show the user the contents of their code, but this behavior scrambles up the file. There's no way to recover the original order. So instead I have to do this gross thing where everything declared in the module is assigned to both the module's scope and some extra JS object, since those do preserve insertion order of key/value entries.

This is so odd to me because I can't think of why anyone would want to access a bunch of code definitions alphabetically. Does the Module tree shaking algorithm actually look at every definition imported, and compare for redundancy against all previous definitions from all modules in a big lookup table, and so the alphabetical sorting speeds up that lookup or something? Did I defeat that by just exporting everything in one object to unpack later, and will that matter?

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Sounds to me like you're using a big stew of holy hell there's a lot of libraries that I'm using, and one of them has strange behaviour or is interacting with some other library that is causing strange behaviour. Because if all you are doing is reading the file to look at the code there is no reason why anything would be any different from exactly the way it was written.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Quite the contrary, I import no libraries and just a couple files that I wrote. Those files are full of es6 class definitions. I'm using Chrome if it's engine dependent but this should be fairly easy behavior for anyone to reproduce.

When I use an es6 "import" statement and place a breakpoint the line right after, all those class definitions in the "Module" type object returned are immediately in alphabetical order. Try it.

As a workaround I'm instead having to do stuff like this to export classes from my "tiny graphics" setup file. That is, wrapping them in an extra object called "tiny":

code:
export const tiny = {};       // We want to access our exports through normal JS objects, not ES Modules, to preserve
                              // the order of definitions in our files (ES Modules alphabetize them). 

const Vec = tiny.Vec =
class Vec extends Float32Array
{
    Definition of class Vec......
}

const Mat = tiny.Mat =
class Mat
{
    Definition of class Mat.......
}
And then

code:
import {tiny} from './tiny-graphics.js';
                                                      // Pull these names into this module's scope for convenience:
const { Vec, Mat, Mat4, Color, Shape, Shader, Overridable, Scene } = tiny;           

......Rest of file.......
That way it's all wrapped in an extra object "tiny" that preserves the order. Because otherwise, if I just make each individual class definition a named export, the order immediately gets lost.

The definitions' order matters since the earlier ones in the file are more basic and fundamental to the codebase, and thus are the ones I want to ease the user into first in the generated tutorial.

Happy Thread fucked around with this message at 20:23 on Mar 17, 2019

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I have no idea what you are trying to do but from a cursory glance you are defining a const, then defining a class with the same variable name. And you're assigning it to a variable in an object and then you're exporting the object within another object and then destructing the object in order to import it.

So that what?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Dumb Lowtax posted:

Why do JavaScript's ES Modules sort their contents alphabetically when you import them? Why would anyone want that? I'm trying to show the user the contents of their code, but this behavior scrambles up the file. There's no way to recover the original order. So instead I have to do this gross thing where everything declared in the module is assigned to both the module's scope and some extra JS object, since those do preserve insertion order of key/value entries.

This is so odd to me because I can't think of why anyone would want to access a bunch of code definitions alphabetically. Does the Module tree shaking algorithm actually look at every definition imported, and compare for redundancy against all previous definitions from all modules in a big lookup table, and so the alphabetical sorting speeds up that lookup or something? Did I defeat that by just exporting everything in one object to unpack later, and will that matter?

I don't understand. Where does this actually cause you trouble? How are you viewing these? There is no inherent ordering to modules members OR to objects. (though that is actually changing, but not something I would depend upon. ES incorporated the de-facto browser behavior for object keys and uses insertion order...unless your keys are integers!)

If you're dependent upon the ordering to your module members or to your objects, then really you're dependent upon a specific implementation and version of your JS interpreter.

It's pretty common for user interfaces to use lexicographic ordering for displaying lists of stuff that has no inherent order since that usually makes more sense then some pseudo random order that will change the next time some underlying implementation-dependent code changes.

I think most people would find lexicographic order the most useful.

Thermopyle fucked around with this message at 22:40 on Mar 17, 2019

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop

Nolgthorn posted:

So that what?

I'm saying that the code is that way because I can't just export my class definitions individually. I have to keep them in BOTH module scope and in an extra object. Module scope so they can see and extend each other, extra object so that their ordering is preserved. That seems to be the only workaround.

In all current major JavaScript engines, objects maintain their contents in order of insertion. This is not likely to change soon and I'm not worried about depending on it for something aesthetic.

An ES Module by comparison does not work that way. Of you attempt to iterate over it you get the members back in alphabetical order.


Thermopyle posted:

I don't understand. Where does this actually cause you trouble? How are you viewing these?

I am iterating through all my class definitions in a certain file to list them out on the HTML page. It's an educational programming site where each little sub-program is mostly made out of a few class definitions, and I want to show those by printing them out.

It makes the most sense to show them in the order they were declared in the file, because that is the intended order of understanding them. It makes sense to ease my users into that codebase starting with the math helper classes and then the geometry helper classes and then the graphics classes and more high level stuff. Not in some other order.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Dumb Lowtax posted:

In all current major JavaScript engines, objects maintain their contents in order of insertion.

This is not exactly true.

Integer-ish keys will be first and they will be sorted in ascending order.

String keys will then come next and they will be insertion order.

Functions next in insertion order.

Then symbols come last and they will be in insertion order.

And then, of course, that ordering is only guaranteed for some methods of iteration on an objects keys and not others, but I can't remember the details on that.


edit: And more to your original point, I don't think there's any standard on how modules should be iterated so someone at Chrome Headquarters just made a reasonable choice to do it lexicographically I guess.

Thermopyle fucked around with this message at 22:57 on Mar 17, 2019

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Right, I'm not talking about integer keys of course. Besides that easy to find exception what I said holds true; these are class definitions with names, not integers, and I wouldn't pack them into an array with some integer keys too.

Iterating with for....of iterates in the same order as my file, and has had the result that I want.

I've been doing it this way successfully, I was wondering why they made this use case so difficult. We're talking about Modules, so they're full of code definitions.
Alphabetical reordering of *code definitions* does not make much sense to me since code is placed into a file in a meaningful order. I think they should have used insertion order for Modules unless there is some benefit to tree shaking that I'm not aware of.

Happy Thread fucked around with this message at 23:06 on Mar 17, 2019

smackfu
Jun 7, 2004

Isn’t your use case extraordinarily rare?

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
I think it affects everyone who uses es6 modules. They get back a data structure that's supposed to be full of code definitions but it's in a strange non-code order.

Even my use case doesn't seem that uncommon, it's just using some introspection into its own source code to print it out

Doom Mathematic
Sep 2, 2008
So to be clear, you're doing something like

JavaScript code:
// numbers.js
export const b = 2
export const a = 1
JavaScript code:
// main.js
import * as numbers from './numbers'

console.log(Object.keys(numbers))
and the problem is you're getting ['a', 'b'] (alphabetical order) when you want it to be ['b', 'a'] (source order).

Are there other programming languages/module systems where the source order of the named exports is significant?

evilfunkyogi
Jun 27, 2005
:effort:
I very genuinely do not mean to "you're doing it wrong" but this:

Dumb Lowtax posted:

I am iterating through all my class definitions in a certain file to list them out on the HTML page.

is not what the es6 module API was designed for.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
If I were trying to render html based on some code in a file, I'd read the file and try to parse it myself. Ie, I'd look for class definitions, I'd even have line numbers if I did it that way so that's what I recommend.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Dumb Lowtax posted:

I think it affects everyone who uses es6 modules.

It doesn't. Most people just import {thing, otherthing} from 'things'; because we don't care about anything else in the module and the reason I know thing and otherthing are in things is that I looked it up in the documentation. Or read the source since a lot of documentation is poo poo in JS-land.

Osmosisch
Sep 9, 2007

I shall make everyone look like me! Then when they trick each other, they will say "oh that Coyote, he is the smartest one, he can even trick the great Coyote."



Grimey Drawer

evilfunkyogi posted:

I very genuinely do not mean to "you're doing it wrong" but this:


is not what the es6 module API was designed for.

Yeah, if you want people to look at code why not just give them the code? Have them browse it in an IDE or on github or whatever.

mystes
May 31, 2006

Osmosisch posted:

Yeah, if you want people to look at code why not just give them the code? Have them browse it in an IDE or on github or whatever.
I guess you had better tell that to every language that has a way to generate html documentation/code listings?

necrotic
Aug 2, 2005
I owe my brother big time for this!
Do they all do it in definition order, or ABC order? I thought ABC order was basically the standard for any doc generation.

Osmosisch
Sep 9, 2007

I shall make everyone look like me! Then when they trick each other, they will say "oh that Coyote, he is the smartest one, he can even trick the great Coyote."



Grimey Drawer

mystes posted:

I guess you had better tell that to every language that has a way to generate html documentation/code listings?

Documentation is not what's being discussed.

If a language has the feature of generating html listings, more power to it. I'd call that wasted effort in the current ecosystem, but whatever. It's also not what Dumb Lowtax is asking about, unless I've completely misunderstood what's going on here (always entirely possible).

RC Cola
Aug 1, 2011

Dovie'andi se tovya sagain
Cross posting this from the modern-front end developer thread.

I'm pretty new at all of this and am still learning. I have 8 hours at work everyday to listen to podcasts and was wondering if there are any that one of you can recommend to me for a beginner. Just to help supplement what I'm learning on my own/through this bootcamp. Maybe ways of thinking, or algorithms, or ways to code cleanly. I'm not sure exactly what I'm even looking for is part of the problem.

Adbot
ADBOT LOVES YOU

PaganGoatPants
Jan 18, 2012

TODAY WAS THE SPECIAL SALE DAY!
Grimey Drawer
Syntax is pretty good: https://syntax.fm/

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply