Skip to content

Commit 2bbba76

Browse files
author
sergey
committed
first commit
0 parents  commit 2bbba76

File tree

11 files changed

+197
-0
lines changed

11 files changed

+197
-0
lines changed

.gitignore

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
target/
2+
projectFilesBackup
3+
project/target/**
4+
.DS_Store
5+
.idea*
6+
.java-version
7+
8+
#user defined conf
9+
application.conf
10+
cristina.conf
11+
12+
### Scala template
13+
*.class
14+
*.log
15+
16+
# sbt specific
17+
.cache
18+
.history
19+
.lib/
20+
dist/*
21+
target/
22+
lib_managed/
23+
src_managed/
24+
project/boot/
25+
project/plugins/project/
26+
27+
# Scala-IDE specific
28+
.scala_dependencies
29+
.worksheet
30+
31+
camel-dir
32+
camel-dir/*
33+

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## 1. compile examples
2+
Open terminal. and execute command `sbt`. This will start *sbt* command prompt
3+
run `~root/fastOptJS`. It will generate javascript from scala code and put it to
4+
`{project_dir}/root/target/scala-2.11/root-fastopt.js`.
5+
6+
## 2. see result
7+
Open `{project_dir}/web/index.html` in browser and press buttons to execute generated code

project/Build.scala

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sbt._, Keys._
2+
3+
import Dependencies._
4+
import org.scalajs.sbtplugin.ScalaJSPlugin
5+
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
6+
7+
object Build extends Build {
8+
9+
val commonSettings = Seq(
10+
organization := "com.github.sonenko.scalajs.samples",
11+
description := "Scala-js samples",
12+
scalaVersion := "2.11.5",
13+
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8", "-feature", "-Xlog-reflective-calls", "-Xfuture", "-Xlint"),
14+
scalaJSStage in Global := FastOptStage
15+
)
16+
17+
18+
lazy val proj = project
19+
.in(file("."))
20+
.aggregate(root)
21+
.settings(commonSettings:_*)
22+
23+
lazy val root = project
24+
.settings(libraryDependencies ++= Seq(
25+
dom, jQuery
26+
))
27+
.settings(commonSettings:_*)
28+
.enablePlugins(ScalaJSPlugin)
29+
}
30+

project/Dependencies.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sbt._
2+
3+
object Dependencies {
4+
val dom = "org.scala-js" % "scalajs-dom_sjs0.6_2.11" % "0.8.0"
5+
val jQuery = "be.doeraene" % "scalajs-jquery_sjs0.6_2.11" % "0.8.0"
6+
}

project/build.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=0.13.7

project/project/ProjectBuild.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package project
2+
3+
import sbt._
4+
import sbt.Keys._
5+
6+
object ProjectBuild extends Build {
7+
lazy val subsBuild = project.in(file(".")).settings(List(
8+
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0"),
9+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.0")
10+
): _*)
11+
}

root/src/main/scala/com/github/sonenko/scalajs/samples/.keep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.sonenko.scalajs.samples
2+
3+
import scala.scalajs.js.JSApp
4+
5+
import com.github.sonenko.scalajs.samples.validatebracesinstring.SampleStringValidator
6+
7+
object Main extends JSApp {
8+
def main(): Unit = ()
9+
10+
SampleStringValidator.init()
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.github.sonenko.scalajs.samples.validatebracesinstring
2+
3+
import scala.scalajs.js
4+
import org.scalajs.jquery._
5+
import js.Dynamic.{ global => g }
6+
7+
object SampleStringValidator {
8+
def init(): Unit = {
9+
jQuery("#validate-braces-in-string").click{onClick _}
10+
11+
def onClick(): Unit ={
12+
val str = g.prompt("enter string to test", """var a = {"a": ["hello", "world", function(){}]}""").toString
13+
g.alert(check(str))
14+
}
15+
}
16+
17+
/**
18+
* checks if str valid line for IDE(for example)
19+
* "{}" => true
20+
* "}{" => false
21+
* "[{]}" => false
22+
*/
23+
def check(str: String): Boolean = {
24+
val opposite: Map[Char, Char] = Map('{' -> '}', '(' -> ')', '[' -> ']')
25+
def doCheck(str: List[Char], expected: List[Char]): Boolean = (str, expected) match {
26+
case (Nil, exp) => exp.length == 0
27+
case ((x@('{' | '[' | '(')) :: xs, e) => doCheck(xs, opposite(x) :: e)
28+
case (('}' | ']' | ')') :: xs, Nil) => false
29+
case ((x@('}' | ']' | ')')) :: xs, eHead :: eTail) => x == eHead && doCheck(xs, eTail)
30+
case (_ :: xs, e) => doCheck(xs.toList, e)
31+
}
32+
doCheck(str.toList, Nil)
33+
}
34+
35+
/**
36+
* --- pure JS realization example
37+
* function check(str) {
38+
* var enters = ['{', '(', '['],
39+
* exits = ['}', ')', ']'],
40+
* opposite = (function(){
41+
* var res = {}, i = 0;
42+
* for (; i < enters.length; i++) { res[enters[i]] = exits[i]; }
43+
* return res;
44+
* }()),
45+
* expected = [],
46+
* key = null,
47+
* char = null;
48+
*
49+
* for (key in str) {
50+
* char = str[key];
51+
* if (enters.indexOf(char) >= 0) {
52+
* expected.unshift(opposite[char]);
53+
* } else if (exits.indexOf(char) >= 0) {
54+
* if (expected.length === 0 || char != expected[0]) return false;
55+
* else expected.shift();
56+
* }
57+
* }
58+
*
59+
* return true;
60+
* }
61+
*/
62+
def checkInLoop(str: String): Boolean = {
63+
val opposite: Map[Char, Char] = Map('{' -> '}', '(' -> ')', '[' -> ']')
64+
var expected: List[Char] = Nil
65+
for { x <- str} {
66+
(x, expected) match {
67+
case ('{' | '[' | '(', e) => expected = opposite(x) :: expected
68+
case ('}' | ']' | ')', Nil) => return false
69+
case ('}' | ']' | ')', eHead :: eTail) if x != eHead => return false
70+
case ('}' | ']' | ')', eHead :: eTail) => expected = eTail
71+
case _ =>
72+
}
73+
}
74+
true
75+
}
76+
}

web/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<button id="validate-braces-in-string">Validate braces in string</button>
9+
10+
<script src="libs/jquery/jquery-2.1.3.min.js"></script>
11+
12+
<script src="../root/target/scala-2.11/root-fastopt.js"></script>
13+
<script>
14+
com.github.sonenko.scalajs.samples.Main();
15+
</script>
16+
17+
</body>
18+
</html>

web/libs/jquery/jquery-2.1.3.min.js

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)