john's tech blog

hope is coming


  • 首页

  • 标签

  • 归档

linux_vi1

发表于 2017-04-11 | 更新于 2019-05-07

linux vi学习笔记1

:wq 保存文件并退出vi

:w 保存文件但不退出vi

:q 不保存文件,退出vi

:$ 跳到文件最后一行

1 按大写的G 跳到最后一行。 然后按小写的O键,增加一行。

2 也可以:
搜索
:set nu (设置行号)
:14 (这里只是示例,是指的最后一行的行号)
o

3 输入 $ 然后按大写的O键 向上增加一行
:$
O

参考资料:
linux vi 在最后一行增加一行
vi/vim 删除:一行, 一个字符, 单词, 每行第一个字符 命令
每天一个Linux命令(48)ping命令

angular1.5.8源码解析之JQLite函数

发表于 2017-04-06 | 更新于 2020-03-25

前言:为了搞清楚上篇文章提到的data函数,我们必须先讲angularjs的JQLite库。
angularjs有个内嵌的轻量级的jquery:jqLite,它的参数只有两种,一种是Dom元素,一种是类似html元素的字符串

JQLite定义

从定义可以看到,JQLite不支持不带html格式的字符串传入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function JQLite(element) {
//如果传入的参数已经是JQLite实例,那直接返回
if (element instanceof JQLite) {
return element;
}
var argIsString;

if (isString(element)) {
element = trim(element);
argIsString = true;
}
//this 有可能就是window
//如果参数是字符串 且不是以<开头
if (!(this instanceof JQLite)) {
if (argIsString && element.charAt(0) != '<') {
throw jqLiteMinErr('nosel', 'Looking up elements via selectors is not supported by jqLite!');
}
return new JQLite(element);
}
//字符串还需要调用jqLiteParseHTML函数解析
if (argIsString) {
jqLiteAddNodes(this, jqLiteParseHTML(element));
} else {
jqLiteAddNodes(this, element);
}
}

如果参数以<开头那还需要

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

## jqLiteParseHTML

```js
//简单的标记正则
var SINGLE_TAG_REGEXP = /^<([\w-]+)\s*\/?>(?:<\/\1>|)$/;

function jqLiteParseHTML(html, context) {
context = context || window.document;
var parsed;

if ((parsed = SINGLE_TAG_REGEXP.exec(html))) {
//parsed[1]是类似 div 的字符串
return [context.createElement(parsed[1])];
}
//制造Fragment
if ((parsed = jqLiteBuildFragment(html, context))) {
return parsed.childNodes;
}

return [];
}

SINGLE_TAG_REGEXP中有几点知识点:

  1. 非获取匹配,匹配pattern但不获取匹配结果,不进行存储供以后使用。这在使用或字符“(|)”来组合一个模式的各个部分时很有用。
    例如“industr(?:y|ies)”就是一个比“industry|industries”更简略的表达式

  2. \1 \2…… 都要和正则表达式集合()一起使用,简单的说就是\1表示重复正则第一个圆括号内匹配到的内容
    \2表示重复正则第二个圆括号内匹配到的内容

  3. ?匹配前面的子表达式零次或一次。例如,“do(es)?”可以匹配“do”或“does”。?等价于{0,1}。

如果符合正则SINGLE_TAG_REGEXP那就通过document.createElement来返回,否则 通过jqLiteBuildFragment
函数来构建html片段,关于jqLiteBuildFragment我们下篇文章再分析。

最后调用jqLiteAddNodes把元素放入JQLite对象中。

jqLiteAddNodes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function jqLiteAddNodes(root, elements) {

if (elements) {
// if a Node (the most common case)
//绝大多数情况是单个节点
if (elements.nodeType) {
root[root.length++] = elements;
} else {
var length = elements.length;

// if an Array or NodeList and not a Window
//如果是数组或者NodeList 且不是window
if (typeof length === 'number' && elements.window !== elements) {
if (length) {
for (var i = 0; i < length; i++) {
root[root.length++] = elements[i];
}
}
} else {
root[root.length++] = elements;
}
}
}
}

我们通过

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

```js
//try to bind to jquery now so that one can write jqLite(document).ready()
//but we will rebind on bootstrap again.
bindJQuery();
```

```js
var bindJQueryFired = false;
function bindJQuery() {
var originalCleanData;

if (bindJQueryFired) {
return;
}
//http://div.io/topic/1154
// bind to jQuery if present;
var jqName = jq(); // 调用jq方法返回jq库名称
jQuery = isUndefined(jqName) ? window.jQuery : // use jQuery (if present)
!jqName ? undefined : // use jqLite
window[jqName]; // use jQuery specified by `ngJq`

if (jQuery && jQuery.fn.on) {
jqLite = jQuery;
//扩展fn函数
//省略部分代码,
//重写了jQuery的cleanData,扩展了jQuery.fn
} else {
jqLite = JQLite;
}

angular.element = jqLite;
// Prevent double-proxying.
//阻止双代理
bindJQueryFired = true;
}

到到这里我们发现angular.element就是jqLite函数。

scala_package

发表于 2017-03-31 | 更新于 2019-05-07

scala 打包子项目

在idea中有多个项目时,我想单独打包子项目,搜了几篇文章受到了启发,

只要在命令行中执行sbt module-a/clean module-a/test命令即可.

其中 module-a 为定义在根项目中的变量.
比如:
lazy val api = (project in file(“api”)).settings(javadocSettings: _*).dependsOn(common, bigdata).aggregate(common, bigdata)

How to execute package for one submodule only on Jenkins?

scala_mock

发表于 2017-03-31 | 更新于 2019-05-07

scala mock

读了这篇文章( Mock Objects for Scala and Android)[https://www.artima.com/scalazine/articles/borachio_mock_objects_for_scala.html],对scala mock有点了解了。

1
2
3
4
5
6
7
8
trait Turtle {
def penUp()
def penDown()
def forward(distance: Double)
def turn(angle: Double)
def getAngle: Double
def getPosition: (Double, Double)
}

This API is not very convenient. We have no way to move to a specific position, instead we need to work out how to get from where we are now to where we want to get by calculating angles and distances. The diagram below, for example, demonstrates the movements a turtle starting at the origin (0, 0) would need to make to draw a line from (1, 1) to (2, 1).

Turtle diagram

You can see an example of code that performs these calculations here. This isn’t trivial, so we want to test to make sure that it’s doing the right thing. Here’s a test (written with ScalaTest) that creates a mock turtle that pretends to start at the origin (0, 0) and verifies that if we ask the code we’ve just written to draw a line from (1, 1) to (2, 1) it performs the correct sequence of turns and movements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MockFunctionTest extends Suite with MockFactory {

val mockTurtle = mock[Turtle]
val controller = new Controller(mockTurtle)

def testDrawLine() {
inSequence {
mockTurtle expects 'getPosition returning (0.0, 0.0)
mockTurtle expects 'getAngle returning 0.0
mockTurtle expects 'penUp
mockTurtle expects 'turn withArgs (~(Pi / 4))
mockTurtle expects 'forward withArgs (~sqrt(2.0))
mockTurtle expects 'getAngle returning Pi / 4
mockTurtle expects 'turn withArgs (~(-Pi / 4))
mockTurtle expects 'penDown
mockTurtle expects 'forward withArgs (1.0)
}

controller.drawLine((1.0, 1.0), (2.0, 1.0))
}
}

scala_abstract_override2

发表于 2017-03-31 | 更新于 2019-05-07

scala abstract override

看到了Scala’s Stackable Trait Pattern这篇文章, 继续研究scala abstract override:

trait Base {
def foo
}

trait StackingTrait extends Base {
abstract override def foo { println ("aa")}
}

class ImplHelper extends Base {
def foo {}
}

class Impl   extends Base{
  def foo {
        println("ss")
  }
}

val implInstance = new Impl with StackingTrait

implInstance.foo

打印出来了aa

This arrangement is frequently needed with traits that implement stackable modifications. To tell the compiler you are doing this on purpose, you must mark such methods as abstract override. This combination of modifiers is only allowed for members of traits, not classes, and it means that the trait must be mixed into some class that has a concrete definition of the method in question.

1…262728…47

John

232 日志
43 标签
GitHub Twitter
欢迎关注我的公众号:沉迷Spring
© 2023 johnwonder
由 Hexo 强力驱动 v3.2.0
|
主题 – NexT.Pisces v7.1.1
|
0%