I'm writing an app using PebbleJS in CloudPebble and would like to have functionality similar to that provided by Moment.js and xdate.js. Specifically, I am interested in:
turning a datetime into a string using a custom format string
adding and subtracting days/weeks/months/years from the current datetime
These modules claim to support CommonJS, so I've tried adding the files to CloudPebble and importing with something like var Moment = require('moment');. When I do this, the app will simply fail to run. I'm not even sure that it compiles.
All I can find in the logs is:
[PHONE] pebble-app.js:?: [PHONESIM] [WARNING] Exception decoding QemuInboundPacket.footer
I see here that moment.js is included in the vendor folder of Pebble.JS. The reasoning for that error must be something universal as I am getting the same error when I try the same thing on a HelloWorld brand new Pebble.JS project on CloudPebble.
I looked at the source code from a pbw export from cloudpebble and it has the full library inside the file. It seems that part of the clock.js file uses moment.js
Look here on how to reference the clock library and inherit the moment.js library at the same time.
Related
I've been playing around with Elm for a couple of days and I wanted to make a port of Moment.JS, since I've seen a lack of libraries for what I wanted, and Moment just has everything that I need.
The thing is that I always face the same error. I have Moment.JS in my Native folder (it is named MomentJS.js) and another file called Moment.js (my wrapper). The problem is that when I call moment in Moment.js, I get an error saying that moment is not defined.
I've tried to import MomentJS.js in my elm file as well, before and/or after Moment.js. I've also tried to copy the whole JS into Moment.js and add my wrapper at the end of it. None of this things worked. You know what could I do? I've been looking for similar repos on the internet but I've never seen a module that has a wrapper and another JS file just for the native library.
This is my Moment.js code:
var _user$project$Native_Moment = (function() {
var moment = require('moment');
var format = function ( format, date ) {
return moment().format();
}
return {
format: format
};
})();
and my Moment.elm code:
module Moment exposing (format)
{-| A module desc
#docs format
-}
import Native.MomentJS
import Native.Moment
{-| Call the default `Moment.js` format method
-}
format : String -> String -> String
format fm dt = Native.Moment.format fm dt
The last thing that I tried was to download Moment from npm, copy its folder from the node_modules folder to my Native folder and do moment = require('moment') but I got TypeError: fun(...) is not a function.
Any suggestions?
After some digging, what you are looking to do is completely possible yes! :) However, it won't be a simple copy paste. Look at the source you posted here, it will require mapping every function to the native elm functions, i'd recommend starting small with this conversion.
First, Get a simple hello world Native example work, see here
Secondly, add in some of the simpler functions from moment.js one by one, i'd recommend starting with moment\src\lib\format\format.js
Lastly, I know this isn't what you want to hear, but if you really want to write javascript in elm, maybe Elm isn't what you are looking for? I really can't imagine converting an entire library similar to Moment.js would be simpler than creating your own Elm library inspired by Moment.js
Either way, best of luck! Seems like a fun challenge either way :)
There are two supported ways for Elm and JavaScript to talk to each other: ports and flags. Both are asynchronous and will be awkward for your needs.
https://guide.elm-lang.org/interop/javascript.html
Should you write native code? Elm's creator says no.
So the best path forward is to use one of the existing time/date libraries or write what you need yourself.
I'm using Flash CC 2014 to create some js/html based interactives. One thing I've come across, is sometimes the published files fail to run. It seems like the export fails to include random clips in the library, so you get errors when it attempts to instantiate them, eg...
this.instance_1 = new lib.c_hand("synched",0);
Uncaught TypeError: undefined is not a function
Has anyone had any similar problems? Is there any way to debug what's going on inside the export script? After a quick look, I can find JSFL files relating to the export, by it appears that they're all obfuscated :\
At the moment I'm resorting to rebuild the library file by hand from GIT diffs. Pain.
It seems that all my questions are destined to have something to do with web essentials. This one is a bit weird. The bundle tag is defined like this:
<bundle minify="true" runOnBuild="true" output="planning-bundle.js">
and the file is named planning.js.bundle,
The thing is that on build it completely ignores the output name and creates a planning.js and planning.min.js instead of planning-bundle.js and planning-bundle.min.js. As a workaround I temporarily change the bundle name to planning-bundle.js.bundle.
I have vs2013 update 2, and latest webessentials. I have looked around in options for webessentials, but I cannot find where this could be controlled...I'd be grateful for any help here.
/Erik
Seems like the schema has changed and that output is abandoned in favor for outputDirectory. See the new format on http://vswebessentials.com/features/bundling. The other attributes are automatically converted to the new format, but output is just removed. So your workaround has become the permanent solution.
I use TypeScript to code my javascript file with Object Oriented Programing.
I want to use the node module https://npmjs.org/package/typescript-require to require my .ts files from other files.
I want to share my files in both server and client side. (Browser) And that's very important. Note that the folder /shared/ doesn't mean shared between client and server but between Game server and Web server. I use pomelo.js as framework, that's why.
For the moment I'm not using (successfully) the typescript-require library.
I do like that:
shared/lib/message.js
var Message = require('./../classes/Message');
module.exports = {
getNewInstance: function(message, data, status){
console.log(requireTs);// Global typescript-require instance
console.log(Message);
return new Message(message, data, status);
}
};
This file need the Message.js to create new instances.
shared/classes/Message.ts
class Message{
// Big stuff
}
try{
module.exports = Message;
}catch(e){}
At the end of the fil I add this try/catch to add the class to the module.exports if it exists. (It works, but it's not really a good way to do it, I would like to do better)
If I load the file from the browser, the module.export won't exists.
So, what I did above is working. Now if I try to use the typescript-require module, I'll change some things:
shared/lib/message.js
var Message = requireTs('./../classes/Message.ts');
I use requireTs instead of require, it's a global var. I precise I'm using .ts file.
shared/classes/Message.ts
export class Message{
// Big stuff
}
// remove the compatibility script at the end
Now, if I try like this and if I take a look to the console server, I get requireTs is object and Message is undefined in shared/lib/message.js.
I get the same if I don't use the export keyword in Message.ts. Even if I use my little script at the end I get always an error.
But there is more, I have another class name ValidatorMessage.ts which extends Message.ts, it's not working if I use the export keyword...
Did I did something wrong? I tried several other things but nothing is working, looks like the typescript-require is not able to require .ts files.
Thank you for your help.
Looking at the typescript-require library, I see it hasn't been updated for 9 months. As it includes the lib.d.ts typing central to TypeScript (and the node.d.ts typing), and as these have progressed greatly in the past 9 months (along with needed changes due to language updates), it's probably not compatible with the latest TypeScript releases (just my assumption, I may be wrong).
Sharing modules between Node and the browser is not easy with TypeScript, as they both use very different module systems (CommonJS in Node, and typically something like RequireJS in the browser). TypeScript emits code for one or the other, depending on the --module switch given. (Note: There is a Universal Module Definition (UMD) pattern some folks use, but TypeScript doesn't support this directly).
What goals exactly are you trying to achieve, and I may be able to offer some guidance.
I am doing the same and keep having issues whichever way I try to do things... The main problems for me are:
I write my typescript as namespaces and components, so there is no export module with multiple file compilation you have to do a hack to add some _exporter.ts at the end to add the export for your library-output.js to be importable as a module, this would require something like:
module.exports.MyRootNamespace = MyRootNamespace
If you do the above it works, however then you get the issue of when you need to reference classes from other modules (such as MyRootNamespace1.SomeClass being referenced by MyRootNamespace2.SomeOtherClass) you can reference it but then it will compile it into your library-output2.js file so you end up having duplicates of classes if you are trying to re-use typescript across multiple compiled targets (like how you would have 1 solution in VS and multiple projects which have their own dll outputs)
Assuming you are not happy with hacking the exports and/or duplicating your references then you can just import them into the global scope, which is a hack but works... however then when you decide you want to test your code (using whatever nodejs testing framework) you will need to mock out certain things, and as the dependencies for your components may not be included via a require() call (and your module may depend upon node_modules which are not really usable with global scope hacking) and this then makes it difficult to satisfy dependencies and mock certain ones, its like an all or nothing sort of approach.
Finally you can try to mitigate all these problems by using a typescript framework such as appex which allows you to run your typescript directly rather than the compile into js first, and while it seems very good up front it is VERY hard to debug compilation errors, this is currently my preferred way but I have an issue where my typescript compiles fine via tsc, but just blows up with a max stack size exception on appex, and I am at the mercy of the project maintainer to fix this (I was not able to find the underlying issue). There are also not many of these sort of projects out there however they make the issue of compiling at module level/file level etc a moot point.
Ultimately I have had nothing but problems trying to wrestle with Typescript to get it to work in a way which is maintainable and testable. I also am trying to re-use some of the typescript components on the clientside however if you go down the npm hack route to get your modules included you then have to make sure your client side uses a require compatible resource/package loader. As much as I would love to just use typescript on my client and my server projects, it just does not seem to want to work in a nice way.
Solution here:
Inheritance TypeScript with exported class and modules
Finally I don't use require-typescript but typescript.api instead, it works well. (You have to load lib.d.ts if you use it, else you'll get some errors on the console.
I don't have a solution to have the script on the browser yet. (Because of export keyword I have some errors client side) I think add a exports global var to avoid errors like this.
Thank you for your help Bill.
I am looking at using http://code.google.com/p/libphonenumber/ for a well-established project. Today the project does not use Google's libraries for JavaScript, favoring jQuery, jQueryUI, requirejs, and so on.
libphonenumber looks awesome ... except that the javascript version (svn co http://libphonenumber.googlecode.com/svn/trunk/javascript/ libphonenumber-js) is laced with goog.require calls. If one runs the demo (libphonenumber-js/i18n/phonenumbers/demo.html if you checked out as suggested) it pulls in tons of google libraries from closure-library.googlecode.com :
GET base.js
GET deps.js
GET error.js
GET string.js
GET asserts.js
GET array.js
GET useragent.js
GET browserfeature.js
GET tagname.js
GET classes.js
GET math.js
GET coordinate.js
GET size.js
GET object.js
GET dom.js
GET json.js
GET util.js
GET descriptor.js
GET fielddescriptor.js
GET message.js
GET serializer.js
GET objectserializer.js
GET stringbuffer.js
GET lazydeserializer.js
GET pbliteserializer.js
I believe if I compile this using the closure compiler ("If you give the use_closure_library parameter a value of true, the compiler looks for goog.require() statements in the source code and supplies the Closure Library code requested by any such statements.", https://developers.google.com/closure/compiler/docs/api-ref) I can cut down the raw number of requests, but this still seems like a rather excessive amount of content for a phone number parser, even a full-featured one.
My question has two possible answers:
A way to use libphonenumber in JavaScript without having to pull in all the Google JavaScript base libraries
An alternate standalone (as in doesn't have dozens of dependencies) first-class phone number processing library with both JavaScript and Java implementations
Any and all suggestions most appreciated.
I've got a custom build (currently 220KB) that I use for my International Telephone Input plugin, with plenty of helper functions exposed. Read the source for details.
You can also use my lib.
https://github.com/Gilshallem/phoneparser
Its only got one method but you can do a lot with it
parsePhone("12025550104");
result: { countryCode:1, areaCode:202, number:5550104, countryISOCode:"US" }
Here are two implementations of Google libphonenumber in JavaScript that have zero dependencies and are implemented in a single file. I've used Nathan Hammond's version without issue but it is not on NPM. Rui Marinho's version is on NPM.
https://github.com/nathanhammond/libphonenumber
https://github.com/ruimarinho/google-libphonenumber
I just spent 2 days figuring this out. For now, anyway, you can download a minified version of libphonenumber-js from here
drop it in place, with the usual
<script type="text/javascript" language="javascript" src="/static/js/libphonenumber-js.min.js"></script>
and get busy coding!
<script>
$(".phone-format").keyup(function () {
var val_old = $(this).val();
var newString = new libphonenumber.AsYouType('US').input(val_old);
$(this).focus().val('').val(newString);
});
</script>